Iterating members within lists within a tuple - list
Imagine having this list
[[1,2,3],[1,2,3],[1,2,3]]
How to get these permutations:
[[1,2,3],[1,2,3],[1,2,3]]
[[1,2,3],[1,2,3],[1,3,2]]
[[1,2,3],[1,2,3],[2,1,3]]
[[1,2,3],[1,2,3],[2,3,1]]
....
[[1,2,3],[1,3,2],[1,2,3]]
[[1,2,3],[1,3,2],[1,3,2]]
[[1,2,3],[1,3,2],[2,1,3]]
....
[[3,2,1],[3,2,1],[3,2,1]]
import itertools
seq = [[1,2,3], [1,2,3], [1,2,3]]
for item in itertools.product(*[itertools.permutations(sublist) for sublist in seq]):
print item
Result:
((1, 2, 3), (1, 2, 3), (1, 2, 3))
((1, 2, 3), (1, 2, 3), (1, 3, 2))
((1, 2, 3), (1, 2, 3), (2, 1, 3))
((1, 2, 3), (1, 2, 3), (2, 3, 1))
((1, 2, 3), (1, 2, 3), (3, 1, 2))
((1, 2, 3), (1, 2, 3), (3, 2, 1))
((1, 2, 3), (1, 3, 2), (1, 2, 3))
((1, 2, 3), (1, 3, 2), (1, 3, 2))
((1, 2, 3), (1, 3, 2), (2, 1, 3))
((1, 2, 3), (1, 3, 2), (2, 3, 1))
((1, 2, 3), (1, 3, 2), (3, 1, 2))
((1, 2, 3), (1, 3, 2), (3, 2, 1))
((1, 2, 3), (2, 1, 3), (1, 2, 3))
((1, 2, 3), (2, 1, 3), (1, 3, 2))
((1, 2, 3), (2, 1, 3), (2, 1, 3))
((1, 2, 3), (2, 1, 3), (2, 3, 1))
((1, 2, 3), (2, 1, 3), (3, 1, 2))
((1, 2, 3), (2, 1, 3), (3, 2, 1))
((1, 2, 3), (2, 3, 1), (1, 2, 3))
((1, 2, 3), (2, 3, 1), (1, 3, 2))
((1, 2, 3), (2, 3, 1), (2, 1, 3))
((1, 2, 3), (2, 3, 1), (2, 3, 1))
((1, 2, 3), (2, 3, 1), (3, 1, 2))
((1, 2, 3), (2, 3, 1), (3, 2, 1))
((1, 2, 3), (3, 1, 2), (1, 2, 3))
((1, 2, 3), (3, 1, 2), (1, 3, 2))
((1, 2, 3), (3, 1, 2), (2, 1, 3))
((1, 2, 3), (3, 1, 2), (2, 3, 1))
((1, 2, 3), (3, 1, 2), (3, 1, 2))
((1, 2, 3), (3, 1, 2), (3, 2, 1))
((1, 2, 3), (3, 2, 1), (1, 2, 3))
((1, 2, 3), (3, 2, 1), (1, 3, 2))
((1, 2, 3), (3, 2, 1), (2, 1, 3))
((1, 2, 3), (3, 2, 1), (2, 3, 1))
((1, 2, 3), (3, 2, 1), (3, 1, 2))
((1, 2, 3), (3, 2, 1), (3, 2, 1))
((1, 3, 2), (1, 2, 3), (1, 2, 3))
((1, 3, 2), (1, 2, 3), (1, 3, 2))
((1, 3, 2), (1, 2, 3), (2, 1, 3))
((1, 3, 2), (1, 2, 3), (2, 3, 1))
((1, 3, 2), (1, 2, 3), (3, 1, 2))
((1, 3, 2), (1, 2, 3), (3, 2, 1))
((1, 3, 2), (1, 3, 2), (1, 2, 3))
((1, 3, 2), (1, 3, 2), (1, 3, 2))
((1, 3, 2), (1, 3, 2), (2, 1, 3))
((1, 3, 2), (1, 3, 2), (2, 3, 1))
((1, 3, 2), (1, 3, 2), (3, 1, 2))
((1, 3, 2), (1, 3, 2), (3, 2, 1))
((1, 3, 2), (2, 1, 3), (1, 2, 3))
((1, 3, 2), (2, 1, 3), (1, 3, 2))
((1, 3, 2), (2, 1, 3), (2, 1, 3))
((1, 3, 2), (2, 1, 3), (2, 3, 1))
((1, 3, 2), (2, 1, 3), (3, 1, 2))
((1, 3, 2), (2, 1, 3), (3, 2, 1))
((1, 3, 2), (2, 3, 1), (1, 2, 3))
((1, 3, 2), (2, 3, 1), (1, 3, 2))
((1, 3, 2), (2, 3, 1), (2, 1, 3))
((1, 3, 2), (2, 3, 1), (2, 3, 1))
((1, 3, 2), (2, 3, 1), (3, 1, 2))
((1, 3, 2), (2, 3, 1), (3, 2, 1))
((1, 3, 2), (3, 1, 2), (1, 2, 3))
((1, 3, 2), (3, 1, 2), (1, 3, 2))
((1, 3, 2), (3, 1, 2), (2, 1, 3))
((1, 3, 2), (3, 1, 2), (2, 3, 1))
((1, 3, 2), (3, 1, 2), (3, 1, 2))
((1, 3, 2), (3, 1, 2), (3, 2, 1))
((1, 3, 2), (3, 2, 1), (1, 2, 3))
((1, 3, 2), (3, 2, 1), (1, 3, 2))
((1, 3, 2), (3, 2, 1), (2, 1, 3))
((1, 3, 2), (3, 2, 1), (2, 3, 1))
((1, 3, 2), (3, 2, 1), (3, 1, 2))
((1, 3, 2), (3, 2, 1), (3, 2, 1))
((2, 1, 3), (1, 2, 3), (1, 2, 3))
((2, 1, 3), (1, 2, 3), (1, 3, 2))
((2, 1, 3), (1, 2, 3), (2, 1, 3))
((2, 1, 3), (1, 2, 3), (2, 3, 1))
((2, 1, 3), (1, 2, 3), (3, 1, 2))
((2, 1, 3), (1, 2, 3), (3, 2, 1))
((2, 1, 3), (1, 3, 2), (1, 2, 3))
((2, 1, 3), (1, 3, 2), (1, 3, 2))
((2, 1, 3), (1, 3, 2), (2, 1, 3))
((2, 1, 3), (1, 3, 2), (2, 3, 1))
((2, 1, 3), (1, 3, 2), (3, 1, 2))
((2, 1, 3), (1, 3, 2), (3, 2, 1))
((2, 1, 3), (2, 1, 3), (1, 2, 3))
((2, 1, 3), (2, 1, 3), (1, 3, 2))
((2, 1, 3), (2, 1, 3), (2, 1, 3))
((2, 1, 3), (2, 1, 3), (2, 3, 1))
((2, 1, 3), (2, 1, 3), (3, 1, 2))
((2, 1, 3), (2, 1, 3), (3, 2, 1))
((2, 1, 3), (2, 3, 1), (1, 2, 3))
((2, 1, 3), (2, 3, 1), (1, 3, 2))
((2, 1, 3), (2, 3, 1), (2, 1, 3))
((2, 1, 3), (2, 3, 1), (2, 3, 1))
((2, 1, 3), (2, 3, 1), (3, 1, 2))
((2, 1, 3), (2, 3, 1), (3, 2, 1))
((2, 1, 3), (3, 1, 2), (1, 2, 3))
((2, 1, 3), (3, 1, 2), (1, 3, 2))
((2, 1, 3), (3, 1, 2), (2, 1, 3))
((2, 1, 3), (3, 1, 2), (2, 3, 1))
((2, 1, 3), (3, 1, 2), (3, 1, 2))
((2, 1, 3), (3, 1, 2), (3, 2, 1))
((2, 1, 3), (3, 2, 1), (1, 2, 3))
((2, 1, 3), (3, 2, 1), (1, 3, 2))
((2, 1, 3), (3, 2, 1), (2, 1, 3))
((2, 1, 3), (3, 2, 1), (2, 3, 1))
((2, 1, 3), (3, 2, 1), (3, 1, 2))
((2, 1, 3), (3, 2, 1), (3, 2, 1))
((2, 3, 1), (1, 2, 3), (1, 2, 3))
((2, 3, 1), (1, 2, 3), (1, 3, 2))
((2, 3, 1), (1, 2, 3), (2, 1, 3))
((2, 3, 1), (1, 2, 3), (2, 3, 1))
((2, 3, 1), (1, 2, 3), (3, 1, 2))
((2, 3, 1), (1, 2, 3), (3, 2, 1))
((2, 3, 1), (1, 3, 2), (1, 2, 3))
((2, 3, 1), (1, 3, 2), (1, 3, 2))
((2, 3, 1), (1, 3, 2), (2, 1, 3))
((2, 3, 1), (1, 3, 2), (2, 3, 1))
((2, 3, 1), (1, 3, 2), (3, 1, 2))
((2, 3, 1), (1, 3, 2), (3, 2, 1))
((2, 3, 1), (2, 1, 3), (1, 2, 3))
((2, 3, 1), (2, 1, 3), (1, 3, 2))
((2, 3, 1), (2, 1, 3), (2, 1, 3))
((2, 3, 1), (2, 1, 3), (2, 3, 1))
((2, 3, 1), (2, 1, 3), (3, 1, 2))
((2, 3, 1), (2, 1, 3), (3, 2, 1))
((2, 3, 1), (2, 3, 1), (1, 2, 3))
((2, 3, 1), (2, 3, 1), (1, 3, 2))
((2, 3, 1), (2, 3, 1), (2, 1, 3))
((2, 3, 1), (2, 3, 1), (2, 3, 1))
((2, 3, 1), (2, 3, 1), (3, 1, 2))
((2, 3, 1), (2, 3, 1), (3, 2, 1))
((2, 3, 1), (3, 1, 2), (1, 2, 3))
((2, 3, 1), (3, 1, 2), (1, 3, 2))
((2, 3, 1), (3, 1, 2), (2, 1, 3))
((2, 3, 1), (3, 1, 2), (2, 3, 1))
((2, 3, 1), (3, 1, 2), (3, 1, 2))
((2, 3, 1), (3, 1, 2), (3, 2, 1))
((2, 3, 1), (3, 2, 1), (1, 2, 3))
((2, 3, 1), (3, 2, 1), (1, 3, 2))
((2, 3, 1), (3, 2, 1), (2, 1, 3))
((2, 3, 1), (3, 2, 1), (2, 3, 1))
((2, 3, 1), (3, 2, 1), (3, 1, 2))
((2, 3, 1), (3, 2, 1), (3, 2, 1))
((3, 1, 2), (1, 2, 3), (1, 2, 3))
((3, 1, 2), (1, 2, 3), (1, 3, 2))
((3, 1, 2), (1, 2, 3), (2, 1, 3))
((3, 1, 2), (1, 2, 3), (2, 3, 1))
((3, 1, 2), (1, 2, 3), (3, 1, 2))
((3, 1, 2), (1, 2, 3), (3, 2, 1))
((3, 1, 2), (1, 3, 2), (1, 2, 3))
((3, 1, 2), (1, 3, 2), (1, 3, 2))
((3, 1, 2), (1, 3, 2), (2, 1, 3))
((3, 1, 2), (1, 3, 2), (2, 3, 1))
((3, 1, 2), (1, 3, 2), (3, 1, 2))
((3, 1, 2), (1, 3, 2), (3, 2, 1))
((3, 1, 2), (2, 1, 3), (1, 2, 3))
((3, 1, 2), (2, 1, 3), (1, 3, 2))
((3, 1, 2), (2, 1, 3), (2, 1, 3))
((3, 1, 2), (2, 1, 3), (2, 3, 1))
((3, 1, 2), (2, 1, 3), (3, 1, 2))
((3, 1, 2), (2, 1, 3), (3, 2, 1))
((3, 1, 2), (2, 3, 1), (1, 2, 3))
((3, 1, 2), (2, 3, 1), (1, 3, 2))
((3, 1, 2), (2, 3, 1), (2, 1, 3))
((3, 1, 2), (2, 3, 1), (2, 3, 1))
((3, 1, 2), (2, 3, 1), (3, 1, 2))
((3, 1, 2), (2, 3, 1), (3, 2, 1))
((3, 1, 2), (3, 1, 2), (1, 2, 3))
((3, 1, 2), (3, 1, 2), (1, 3, 2))
((3, 1, 2), (3, 1, 2), (2, 1, 3))
((3, 1, 2), (3, 1, 2), (2, 3, 1))
((3, 1, 2), (3, 1, 2), (3, 1, 2))
((3, 1, 2), (3, 1, 2), (3, 2, 1))
((3, 1, 2), (3, 2, 1), (1, 2, 3))
((3, 1, 2), (3, 2, 1), (1, 3, 2))
((3, 1, 2), (3, 2, 1), (2, 1, 3))
((3, 1, 2), (3, 2, 1), (2, 3, 1))
((3, 1, 2), (3, 2, 1), (3, 1, 2))
((3, 1, 2), (3, 2, 1), (3, 2, 1))
((3, 2, 1), (1, 2, 3), (1, 2, 3))
((3, 2, 1), (1, 2, 3), (1, 3, 2))
((3, 2, 1), (1, 2, 3), (2, 1, 3))
((3, 2, 1), (1, 2, 3), (2, 3, 1))
((3, 2, 1), (1, 2, 3), (3, 1, 2))
((3, 2, 1), (1, 2, 3), (3, 2, 1))
((3, 2, 1), (1, 3, 2), (1, 2, 3))
((3, 2, 1), (1, 3, 2), (1, 3, 2))
((3, 2, 1), (1, 3, 2), (2, 1, 3))
((3, 2, 1), (1, 3, 2), (2, 3, 1))
((3, 2, 1), (1, 3, 2), (3, 1, 2))
((3, 2, 1), (1, 3, 2), (3, 2, 1))
((3, 2, 1), (2, 1, 3), (1, 2, 3))
((3, 2, 1), (2, 1, 3), (1, 3, 2))
((3, 2, 1), (2, 1, 3), (2, 1, 3))
((3, 2, 1), (2, 1, 3), (2, 3, 1))
((3, 2, 1), (2, 1, 3), (3, 1, 2))
((3, 2, 1), (2, 1, 3), (3, 2, 1))
((3, 2, 1), (2, 3, 1), (1, 2, 3))
((3, 2, 1), (2, 3, 1), (1, 3, 2))
((3, 2, 1), (2, 3, 1), (2, 1, 3))
((3, 2, 1), (2, 3, 1), (2, 3, 1))
((3, 2, 1), (2, 3, 1), (3, 1, 2))
((3, 2, 1), (2, 3, 1), (3, 2, 1))
((3, 2, 1), (3, 1, 2), (1, 2, 3))
((3, 2, 1), (3, 1, 2), (1, 3, 2))
((3, 2, 1), (3, 1, 2), (2, 1, 3))
((3, 2, 1), (3, 1, 2), (2, 3, 1))
((3, 2, 1), (3, 1, 2), (3, 1, 2))
((3, 2, 1), (3, 1, 2), (3, 2, 1))
((3, 2, 1), (3, 2, 1), (1, 2, 3))
((3, 2, 1), (3, 2, 1), (1, 3, 2))
((3, 2, 1), (3, 2, 1), (2, 1, 3))
((3, 2, 1), (3, 2, 1), (2, 3, 1))
((3, 2, 1), (3, 2, 1), (3, 1, 2))
((3, 2, 1), (3, 2, 1), (3, 2, 1))
Related
How can I view all of the list items in the smlnj shell?
I have a list with 365 items, corresponding to days in a year. I need to compare my results with a manually calculated result, but all I can see is: val it = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,...] : int list Is there a way to do this without writing a helper function?
As quoify suggests, you could increase the depth at which SML/NJ will render a very big value. But you can also compare using = without being able to see the end of the list. For example, say your 365-length list is called days, and your manually calculated result is called check, then in the REPL upon inspection, it just looks like a lot of 1s in both: - days; > val it = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...] : int list - check; > val it = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...] : int list Using = on these values will show if there actually is the same amount of elements and those elements are pairwise actually the same. - days = check; > val it = true : bool If days and check contained a different amount of elements, or if a 1 in the ... part was actually a 0, then comparing them for equality would return false instead. So you don't really need to be able to see 365 values on the screen if you can programmatically verify that they're equivalent. This is a lot less error-prone anyways. Is there a way to do this without writing a helper function? As above; but I'd like to ask: What's so bad about a helper function here? I think if you employ a helper function, you will reduce risks of accidentally typing 364 or 367 1s into your manually checked value. Here's a function that generates a list of n copies of x: fun repeat 0 x = [] | repeat n x = x :: repeat (n-1) x Then you can make a function that checks your days instead: fun does_it_have_365_1s xs = xs = repeat 365 1 Running this in the REPL: - does_it_have_365_1s [1,1,1]; > val it = false : bool - does_it_have_365_1s [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]; > val it = true : bool Oh god, did I really type that. Let's flip a single 1 to a 0. - does_it_have_365_1s [1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]; > val it = false : bool
How to index a mesh of points in 3D space to draw with OpenGL GL_LINES?
Let's say I have 8 positions representing the vertices of a polyhedron: (0, 0, 0), (1, 0, 0), (1, 0, 1), (0, 0, 1), (0, 1, 0), (1, 1, 0), (1, 1, 1), (0, 1, 1), How to efficiently generate a list of pairs of indices representing the edges of this mesh? [ (0, 1), (1, 2), ... ] The main goal is to draw the mesh with OpenGL using the GL_LINES primitive.
Sum of 100 digit numbers
I have two arrays each containing 100 digits.I need to add corresponding digits from units place meaning from the end of the array.If there is a carry it should be added in the next index.It should return another array containing the sum. Here is my code and something seems to be wrong .Help me void sumOf100DigitNumbers(int num1[100], int num2[100], int sum[101]) { int i, j = 100, k; for (i = 99; i >= 0; i--) { if (sum[j] == 1) { } else sum[j] = 0; k = sum[j] + num1[i] + num2[i]; if (k >= 10) { sum[j] = k % 10; sum[j - 1] = 1; } else { sum[j] = k; } j--; } } Method to test it [TestMethod, Timeout(1000)] void Test_sumOf100DigitNumbers2() { int a[100] = {6, 8, 3, 7, 8, 1, 2, 4, 6, 7, 2, 0, 6, 6, 7, 0, 0, 9, 4, 8, 2, 9, 6, 3, 1, 7, 2, 3, 0, 4, 4, 5, 0, 9, 7, 0, 8, 9, 2, 6, 9, 2, 8, 8, 0, 2, 8, 2, 6, 5, 3, 0, 5, 2, 2, 5, 8, 8, 6, 6, 2, 3, 6, 0, 7, 0, 9, 9, 0, 4, 6, 4, 0, 4, 5, 1, 9, 5, 1, 5, 3, 6, 6, 3, 2, 4, 0, 7, 7, 8, 4, 6, 8, 7, 8, 9, 1, 6, 9, 2}; int b[100] = { 4, 1, 4, 1, 5, 0, 1, 8, 4, 5, 9, 7, 6, 2, 2, 0, 1, 7, 2, 5, 0, 3, 6, 9, 0, 8, 7, 3, 0, 2, 7, 8, 6, 5, 7, 3, 6, 8, 4, 2, 9, 2, 4, 8, 2, 1, 1, 0, 6, 6, 2, 7, 2, 8, 9, 7, 2, 4, 2, 2, 7, 6, 0, 7, 2, 3, 8, 4, 2, 5, 4, 7, 1, 8, 9, 9, 7, 0, 3, 2, 5, 1, 9, 7, 1, 0, 0, 0, 2, 1, 1, 5, 9, 0, 0, 1, 6, 6, 9, 7 }; int ans[101] = { 1, 0, 9, 7, 9, 3, 1, 4, 3, 1, 3, 1, 8, 2, 8, 9, 0, 2, 6, 7, 3, 3, 3, 3, 2, 2, 5, 9, 6, 0, 7, 2, 3, 7, 5, 4, 4, 5, 7, 6, 9, 8, 5, 3, 6, 2, 3, 9, 3, 3, 1, 5, 7, 8, 1, 2, 3, 1, 2, 8, 8, 9, 9, 6, 7, 9, 4, 8, 3, 3, 0, 1, 1, 2, 3, 5, 1, 6, 5, 4, 7, 8, 8, 6, 0, 3, 4, 0, 7, 9, 9, 6, 2, 7, 7, 9, 0, 8, 3, 8, 9 }; int c[101]; sumOf100DigitNumbers(a, b, c); Assert::AreEqual(true, areEqualArrays(ans, c, 101), L"sumOf100DigitNumbers() failed", 1, 2); }; but my output is as follows: Output
You never initialize c. This means that its contents are indeterminate. You can't predict what values it will contain. So when you do this: if (sum[j] == 1) If one of those indeterminate values happens to be 1 then you're performing a carry you don't intend on doing. You can initialize it as follows: int c[101] = { 0 }; Then you can remove this entirely: if (sum[j] == 1) { } else sum[j] = 0;
How do I get a UCS code of a 1-byte letter of UTF-8 in C++?
I need to check, whether a letter (in english and russian languages) is alphabetical. A file is supposed to be encoded with UTF-8 by default. I found out, that the best solution is working with UCS codes. The way to calculate UCS-code of 2-bytes encoded letter is #include <stdio.h> #include <stdlib.h> char utf8len[256] = { // len = utf8len[c] & 0x7 cont = utf8len[c] & 0x8 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0 - 15 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 16 - 31 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 32 - 47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 48 - 63 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 80 - 95 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 112 - 127 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 80 - 8f 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 90 - 9f 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // a0 - af 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // b0 - bf 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // c0 - cf 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // d0 - df 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // e0 - ef 4, 4, 4, 4, 4, 4, 4, 4, // f0 - f7 5, 5, 5, 5, // f8, f9, fa, fb 6, 6, // fc, fd 0, 0 // fe, ff }; #define UTF8LEN(c) (utf8len[(unsigned char)(c)] & 0x7) #define UTF8CONT(c) (utf8len[(unsigned char)(c)] & 0x8) int main (int argc, char *argv[]) { char *s = "Б№1АГД"; //string which contains cyrillic symbols while (*s) { int ucode; printf ("[%s] %d\n", s, UTF8LEN(*s)); if ((UTF8LEN(*s) == 2) && UTF8CONT(s[1])) { ucode = ((*s & 0x1f) << 6) | (s[1] & 0x3f); //! HERE I GET UCS CODE printf ("ucode = 0x%x\n", ucode); s++; } s++; } } It's a half of the solution I'm looking for. This code alows me to work with cyrillic symbols only (as they're encoded with 2 bytes in UTF-8). The problem is, I need to work with latin alphabet as well. So what should i do to get UCS code for 1-byte symbol (in my case with UTF8LEN(c)=1)? Upd: Probably, the solution is: ucode = *s Will this work?
Flattening a list with a nested dictionary
I have a list that includes a nested dictionary: l =[('M', 1, 2, {'t': (2, 1.0)}), ('L', 2, 4, {'b': (4, 0.25), 'fi': (4, 0.75)}), ('J', 4, 5, {'a': (5, 0.2), 'w': (5, 0.2), 'wh': (5, 0.4), 'en': (5, 0.2)}), ('T', 4, 6, {'sl': (6, 0.5), 'f': (6, 0.1), 'pz': (6, 0.17), 'al': (6, 0.1)}), ('P', 5, 5, {'tr': (5, 0.2), 'in': (5, 0.2), 'fa': (5, 0.2), 'if': (5, 0.2)})] I would like to flat out this list, in order to have a plain list like this: [('M', 1, 2, 't', 2, 1.0, 'L', 2, 4, 'b', 4, 0.25, 'fi', 4, 0.7), ('J', 4, 5, 'a', 5, 0.2, 'w', 5, 0.2, 'wh', 5, 0.4, 'en', 5, 0.2)] I tried some flatten functions, but I got confused with how to flatten the dictionary within the list. I am new to python, as you can tell. Could anyone help me with getting around this.
I could only think of below brute force method... your given iterator: d =[('M', 1, 2, {'t': (2, 1.0)}), ('L', 2, 4, {'b': (4, 0.25), 'fi': (4, 0.75)}), ('J', 4, 5, {'a': (5, 0.2), 'w': (5, 0.2), 'wh': (5, 0.4), 'en': (5, 0.2)}), ('T', 4, 6, {'sl': (6, 0.5), 'f': (6, 0.1), 'pz': (6, 0.17), 'al': (6, 0.1)}), ('P', 5, 5, {'tr': (5, 0.2), 'in': (5, 0.2), 'fa': (5, 0.2), 'if': (5, 0.2)})] My solution: l = [ ] for item in d: for i in item: if type(i) is dict: for j in i.items(): for p in j: l.append(p) if not isinstance(p,tuple) else [l.extend(k for k in p)] else: l.append(i) print l output: ['M', 1, 2, 't', 2, 1.0, 'L', 2, 4, 'fi', 4, 0.75, 'b', 4, 0.25, 'J', 4, 5, 'a', 5, 0.20000000000000001, 'en', 5, 0.20000000000000001, 'w', 5, 0.20000000000000001, 'wh', 5, 0.40000000000000002, 'T', 4, 6, 'pz', 6, 0.17000000000000001, 'f', 6, 0.10000000000000001, 'al', 6, 0.10000000000000001, 'sl', 6, 0.5, 'P', 5, 5, 'fa', 5, 0.20000000000000001, 'if', 5, 0.20000000000000001, 'tr', 5, 0.20000000000000001, 'in', 5, 0.20000000000000001] Hope this helps :)