Good day, Dynamo Forum! This will be a long post ahead, so I humbly ask for your patience and kindness. I’m relatively new to DesignScript, and I’ve taken the big step of challenging myself with writing a function to build my skills and confidence.
The Problem: To write a function to replace duplicates for a list.
I spent a whole day crafting and refining a flowchart to explain the flow and logic of the script, and another day attempting to reflect it properly in the code block:
The logic of the program is designed to:
a. Execute a loop as long as the number of unique items is less than the list count.
b. Check if an item (the Current Item) has a duplicate, starting from the first value.
c. If the Current Item has a duplicate, it will change the value of the duplicate by increasing it with 1. No change will be done to the Current Item.
d. If the Current Item is in the middle of the list and has a duplicate before the middle of the list, the Current Item will be compared with the item next to it until the last item, and then with the first item in the list until it reaches the duplicate or the Current Item itself (if there is no duplicate).
e. If it has no duplicate, it will set the item next to the Current Item as the new Current Item. (If I understand correctly, imperative code blocks specifically allow the logic of points c & e.)
f. It will do these repeatedly until it reaches the last item. Once it reaches the last item (now the new Current Item) it will compare it to all other items to check for duplicates, starting the the 1st item of the list.
g. If there are still duplicates after comparing the last item (i.e., if # unique items < list count), it will start over from the first item of the updated list.
h. The program will terminate ONLY if there are no more duplicates, and then display the final updated list.
And here is the code block:
Questions:
- Are the values in the list being properly updated in this script? (I don’t think it is, and I think I’m missing a line to properly update/overwrite the existing values in the list.)
1.1. Am I misunderstanding how imperative blocks update values in lists?
1.2. Should I use “List.ReplaceItemAtIndex” instead? - Are the “return CI” lines unnecessary? (I think this is so, please confirm or correct me. I initially thought those lines would return the Current Values and update the list, until there are no more duplicates)
2.1. Should I actually return anything else within the imperative code block other than the updated list?
If there is any other problem that you can see, please tell me. Although programming is definitely not my comfort zone, I actually enjoyed dissecting the problem and trying to reflect it in the code. I’m sure I would enjoy programming more if I get over this hurdle, so any help would mean a lot to me.
Thank you!
Edit 1: Changed the photo of the flowchart, since the initial one was a wrong copy.