r/dailyprogrammer • u/MasterAgent47 • Nov 21 '17
[2017-11-21] Challenge #341 [Easy] Repeating Numbers
Description
Locate all repeating numbers in a given number of digits. The size of the number that gets repeated should be more than 1. You may either accept it as a series of digits or as a complete number. I shall explain this with examples:
11325992321982432123259
We see that:
- 321 gets repeated 2 times
- 32 gets repeated 4 times
- 21 gets repeated 2 times
- 3259 gets repeated 2 times
- 25 gets repeated 2 times
- 59 gets repeated 2 times
Or maybe you could have no repeating numbers:
1234565943210
You must consider such a case:
9870209870409898
Notice that 987 repeated itself twice (987, 987) and 98 repeated itself four times (98, 98, 987 and 987).
Take a chunk "9999". Note that there are three 99s and two 999s.
9999 9999 9999
9999 9999
Input Description
Let the user enter 'n' number of digits or accept a whole number.
Output Description
RepeatingNumber1:x RepeatingNumber2:y
If no repeating digits exist, then display 0.
Where x and y are the number of times it gets repeated.
Challenge Input/Output
| Input | Output |
|---|---|
| 82156821568221 | 8215682:2 821568:2 215682:2 82156:2 21568:2 15682:2 8215:2 2156:2 1568:2 5682:2 821:2 215:2 156:2 568:2 682:2 82:3 21:3 15:2 56:2 68:2 |
| 11111011110111011 | 11110111:2 1111011:2 1110111:2 111101:2 111011:3 110111:2 11110:2 11101:3 11011:3 10111:2 1111:3 1110:3 1101:3 1011:3 0111:2 111:6 110:3 101:3 011:3 11:10 10:3 01:3 |
| 98778912332145 | 0 |
| 124489903108444899 | 44899:2 4489:2 4899:2 448:2 489:2 899:2 44:3 48:2 89:2 99:2 |
Note
Feel free to consider '0x' as a two digit number, or '0xy' as a three digit number. If you don't want to consider it like that, it's fine.
If you have any challenges, please submit it to /r/dailyprogrammer_ideas!
Edit: Major corrections by /u/Quantum_Bogo, error pointed out by /u/tomekanco
4
u/tomekanco Nov 21 '17
Could you clarify the special case? I don't see how these outputs follow this rule.
2
u/Quantum_Bogo Nov 21 '17 edited Nov 21 '17
Python 3.6
Answers:
2
u/tomekanco Nov 21 '17 edited Nov 21 '17
Python 3.6
Output
21
u/Godspiral 3 3 Nov 21 '17
in J,
9
u/thorwing Nov 21 '17
Java 9
creates output in the form:
13
u/lukz 2 0 Nov 21 '17
Brainfuck
Prints only two-digit repeated numbers. Output format is: repeat_count*number, separated by spaces.
Tested in interpreter.
Sample session
Code:
6
u/skeeto -9 8 Nov 21 '17 edited Nov 21 '17
C, storing all substrings up to a certain length, N, in an array. Then it sorts the array and counts runs.
6
u/zqvt Nov 22 '17 edited Nov 22 '17
Haskell
output
6
u/nyanaki Nov 27 '17 edited Nov 27 '17
In Python 2.7:
Outputs:
1
u/MasterAgent47 Nov 21 '17
It's in the output table. Yes it was incorrect but I corrected it a few minutes ago. It's all good now.
4
u/[deleted] Nov 21 '17
Javascript
output:
Doesn't print anything on no substrings, that's trivial to fix.
1
u/Purlox Nov 21 '17
Do these pieces of information not contradict each other?
The way I understood the first is that if all occurances of a pattern (98 in this case) are also inside all occurances of some bigger pattern (987 in this case), then you should only report the bigger pattern. If that's correct then the suggested output, that I quoted, should only have
4489:2and have no448:2.2
u/popillol Nov 21 '17 edited Nov 21 '17
Go / Golang Playground Link
Output
2
u/[deleted] Nov 21 '17 edited Nov 21 '17
Ruby
Should work with any input, but considers spaces as a character when taking other input (like sentences, etc.)
Output:
2
u/gabyjunior 1 2 Nov 21 '17 edited Nov 22 '17
C
Takes the input string as program argument, works with any sequence of characters (not only numbers).
EDIT String comparisons replaced by incremental comparison character by character, and the program now takes the sequence minimal length as second argument.
Output
2
u/JD7896 Nov 21 '17
Python 3.6
Wanted to write something as short as possible.
2
u/buddycool Nov 22 '17
Java
2
u/[deleted] Nov 24 '17
Python 3.6
This was my first r/dailyprogrammer attempt. I don't think my solution is very elegant at all but it was fun working on it, I look forward to doing more! Feedback is definitely welcome if anyone cares to read this haha
Output
2
u/rc48 Nov 24 '17
Clojure
1
u/Scara95 Nov 21 '17
I think your output is wrong: 82156821568221 82156821568221
edit: i saw you correct it nevermind
1
u/Scara95 Nov 21 '17 edited Nov 21 '17
C++ with a trie
edit: added io on main, does not respect 0 output constraint
Input:
Output:
1
u/TheBlackCat13 Nov 21 '17 edited Nov 21 '17
Python 3.6
Edit: fixed typo
1
u/KeinBaum Nov 21 '17 edited Nov 21 '17
Scala, O(n2)
1
u/LegendK95 Nov 21 '17 edited Nov 21 '17
Rust, works for any sequence of characters
1
u/MattieShoes Nov 21 '17
Go, simply building a map of all substrings and their frequency, and printing out the ones that occur more than once.
Output:
1
u/Daanvdk 1 0 Nov 21 '17 edited Nov 21 '17
Haskell
Prints an empty line instead of 0 when there are no repeating numbers, makes more sense to me. Also it doesn't really care if the string actually only contains digits.
1
u/svgwrk Nov 21 '17
I couldn't think of any elegant way to do this. I'm sure there is some fancy way of going about it, but I used brute force and a hashmap. Similar to the solution by /u/LegendK95, this one works for arbitrary text rather than numbers. It honestly runs faster than I would have expected, too.
In keeping with my usual preference for abstraction, I have written a subarray iterator thingy rather than just writing a loop. This means that practically all of the real thinking went into the
next()function of the iterator.1
u/Scroph 0 0 Nov 21 '17
D solution, though it's still a work in progress :
1
u/thestoicattack Nov 21 '17 edited Nov 21 '17
C++17. O(n2 )log(n), I think, because of
std::map.1
u/Daanvdk 1 0 Nov 21 '17
Python3
1
u/TheMsDosNerd Nov 21 '17
Python 3
1
u/mn-haskell-guy 1 0 Nov 21 '17
Javascript - standard counting using a hash, but it stops as soon as longer repeating substrings are not possible.
1
u/sunnset Nov 21 '17 edited Nov 22 '17
JAVA:
1
u/[deleted] Nov 21 '17
COBOL:
1
u/pop-pop-pop-pop-pop Nov 22 '17
JavaScript, only accepts strings because the unusual way JS handles big numbers. Criticism welcome.
Output:
1
u/mn-haskell-guy 1 0 Nov 22 '17
Javascript, but no use of objects / hashes -- only arrays. Could easily be translated to C.
Uses O(n) space. Running time is O(n2 ) but is highly dependent on the number of repeated substrings -- the fewer repeated substrings the faster the algorithm runs.
1
u/pkoepke Nov 22 '17
JavaScript. At first I was going to build a map and iterate over the input more than once, but later I realized that it's faster to check if the substring has appeared before as it builds the map. Requires that the input be a string, not a number, but works for arbitrary strings.
Code: var possibleStringsObject = {}
Output:
1
u/PolarBearITS Nov 22 '17
Python 3.6
Output
1
u/minikomi Nov 22 '17 edited Nov 22 '17
Clojure:
Soultion:
Formatting response:
Results:
1
u/narcodis Nov 22 '17
Javascript
Output
1
u/g00glen00b Nov 22 '17 edited Nov 22 '17
JavaScript:
Input should be the numbers as a string, considering that "124489903108444899" cannot be represented as a number due to number precision.
1
u/[deleted] Nov 22 '17
Python 3.6
output:
Feedback appreciated
1
u/Scara95 Nov 22 '17
Scheme using trie works for any sequence of characters.
Mostly r6rs compilant: should work if you add a syntax definition for rec and the needed imports like that:
NB. eq is not guaranteed by r6rs to return #t for same code characters, but it seems to work like that on implementations I tested
Works out of the box on chez scheme and with added syntax on guile
1
u/empirep Nov 22 '17
Python 3:
1
u/FANS4ever Nov 22 '17
Python 3
Output sample:
1
u/jnazario 2 0 Nov 22 '17
F#. builds a sequence of all substrings and then computes frequencies of them, then filters for length > 1 and frequency > once.
1
u/jastify Nov 22 '17
First time coding in Scala, I like the weird shortcuts, trying to get into the more scala-specific stuff later.
sample interactions:
1
u/Taselod Nov 23 '17 edited Nov 23 '17
I think I got it.. but the example doesn't give all of the possible outputs...and i don't think the examples do either?
Input: 11111011110111011 Output:
Input: 82156821568221 Output:
1
u/[deleted] Nov 23 '17
F#
This can be run directly in FSI.exe which will output a list of tuples:
Output (FSI.exe):
In order to actually "complete" the challenge:
1
u/yeah_i_got_skills Nov 23 '17 edited Nov 23 '17
Powershell:
Output:
1
u/Mellolian Nov 23 '17
Python 3.6 with no imports: Input
Output
1
u/chunes 1 2 Nov 23 '17
Factor
Output:
1
u/Shosty123 Nov 24 '17 edited Nov 24 '17
JavaScript
1
u/volleythrowaway34343 Nov 25 '17 edited Nov 25 '17
in python, i kinda complicated the output in the 2nd fuction, but meh (EDIT: forgot about the case when theres 0 duplicates)
1
u/CoconutOfEuboea Nov 25 '17 edited Nov 26 '17
Hi I think I did it in my own nooby way! - PYTHON 3.4.3
Output for first challenge:
If you want to avoid the overlap of repeated strings of numbers, where:
input 3434 outputs 34:2, 43: 1
where you'd rather an output of just 34:2
then for line 9 of my code, change
for i in range(0,length):tofor i in range(0,length,k):1
u/Lachiko Nov 26 '17
in Javascript,
1
u/__dict__ Nov 26 '17
Racket scheme
After playing around with Clojure I just need my threading macros. Haven't been able to get curly-fn (https://docs.racket-lang.org/curly-fn/index.html) to work yet though :(
1
u/ndydl Nov 27 '17
Perl 5
1
u/dsax7 Nov 28 '17
Fairly new to programming here and here is my solution using Python 3.6.3
I feel like Counter could have been used for an easier solution, but I was kinda lazy to think about it. Would love to get some feedback!
1
u/Mega325 Nov 29 '17
My implementation using C#
//Counting all repeating numbers within an interger string
}
1
u/Hezip Nov 29 '17
JAVA 8 First time posting here, hope i got the formatting right for a reddit post. Anyways, I used arrays to store grouped up digits and compare for repeating ones. Not the most efficient way but sure i'm only a learner.
removing the optional System.out.print that I used to keep track gives an output of :
1
u/VeaArthur Nov 30 '17
C#
1
u/joffajoffa Dec 01 '17
C#
1
u/[deleted] Dec 04 '17
Help: This only finds 44 twice when it should be three times. I don't know how to fix it. Any ideas?
1
u/MoX46 Dec 06 '17
JavaScript
1
u/Specter_Terrasbane Dec 08 '17 edited Dec 08 '17
Python 2
... or, if you really want the output sorted the same way as in the specification (i.e. most digits first, then by position of first occurrence) :
1
u/Scara95 Dec 08 '17 edited Dec 09 '17
Q Well, not my first solution, it's late too, but hey, learning Q and doing simple tasks to improve. I have a solution, why not posting it?
Use:
Using example output:
edit: much better repeating implementation (with slightly changed output format):
edit: yet another implementation
1
u/Sonnenhut Dec 10 '17 edited Dec 10 '17
Kotlin
1
u/[deleted] Dec 10 '17 edited Dec 10 '17
I didn't like the output format for use of the output in the Powershell pipeline.
Powershell:
Output:
1
u/tester4u Dec 18 '17
Python 3.6
Output
1
u/ivankahl Dec 20 '17
My solution in C#
1
u/[deleted] Dec 24 '17 edited Dec 24 '17
Javascript
Output
1
u/alemsa Dec 24 '17
Javascript:
1
u/zatoichi49 Dec 27 '17 edited Dec 27 '17
Method:
Convert each number from an integer to a string, and create a list of all concurrent digits for each size of grouping within each string. Then create a dictionary to count the instances of each digit group within the list. Print 0 if the dictionary is empty (or null), if not then print the dictionary.
Python 3:
Output:
1
u/KeenWolfPaw Dec 30 '17
Spent about 5 or so days on this one. Checking output and checking all patterns at runtime. Let me know if you can find a formula for the max amount of independent repeating patterns of size 2 possible from n digits, I just settled for 2*input.
C
1
u/BTRBT Dec 31 '17 edited Dec 31 '17
Coded in python 2.7
I'm rather new to programming, so advice is certainly appreciated.
1
u/mochancrimthann Jan 05 '18 edited Jan 05 '18
Elixir
Please feel free to provide feedback, I'm quite new at the language. This should work with numbers and letters.
1
u/DEN0MINAT0R Jan 11 '18
Python 3
It's a little messy, but it works.
1
u/crudkin Jan 29 '18
In Python 3.6:
1
u/__8021m1208__ Mar 26 '18
Python 3.6
1
u/[deleted] Apr 16 '18
Here is my code using python 3.6 :
1
u/vorboto Apr 27 '18
I think I got it but when entering in the longer numbers I am getting an error from my input method.
It's in java.
1
u/akojic Apr 27 '18 edited Apr 27 '18
in PHP
1
u/vorboto Apr 28 '18
Actually I think I got it by using strings instead of integers.
1
u/nikit9999 Nov 21 '17
C#
0
u/[deleted] Nov 21 '17
[deleted] — view deleted comment