0

Find Matching pairs of numbers

How could code be written to output a number for every two numbers that are identical? For example given 223323436577 would print out 2 3 7.

29th May 2018, 9:41 PM
Andries van der Walt
3 ответов
+ 2
'''With 2 numbers that are identical I assume you mean 2 characters that are next to each other that are the same In that case, try this in Python.''' '''Find Matching pairs of numbers How could code be written to output a number for every two numbers that are identical? For example given 223323436577 would print out 2 3 7.''' inp='223323436577' print(*[inp[i] for i in range(len(inp)-1) if(inp[i]==inp[i+1])])
30th May 2018, 3:30 AM
Louis
Louis - avatar
+ 1
You have not mentioned the language but in python its very simple, l=set(map(int,list(input()))) for i in l: print(i,end=' ') Here we have split the input and the set converts all the same type of digits into a single value or in short deletes all duplicate values from the input.
30th May 2018, 2:15 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
The best way, in my opinion, would be to convert whatever it is into a String, then run it through a Regular Expression Comparison. Find recurring numbers, assign them to an array then print the result. For a step-interval of 2 if looping through, use your language's equivalent of the 'continue' statement to repeat without execution.
29th May 2018, 9:46 PM
ghostwalker13
ghostwalker13 - avatar