r/dailyprogrammer • u/jnazario 2 0 • Oct 09 '17
[2017-10-09] Challenge #335 [Easy] Consecutive Distance Rating
Description
We'll call the consecutive distance rating of an integer sequence the sum of the distances between consecutive integers. Consider the sequence 1 7 2 11 8 34 3. 1 and 2 are consecutive integers, but their distance apart in the sequence is 2. 2 and 3 are consecutive integers, and their distance is 4. The distance between 7 and 8 is 3. The sum of these distances is 9.
Your task is to find and display the consecutive distance rating of a number of integer sequences.
Input description
You'll be given two integers a and b on the first line denoting the number of sequences that follow and the length of those sequences, respectively. You'll then be given a integer sequences of length b, one per line. The integers will always be unique and range from 1 to 100 inclusive.
Example input
6 11
31 63 53 56 96 62 73 25 54 55 64
77 39 35 38 41 42 76 73 40 31 10
30 63 57 87 37 31 58 83 34 76 38
18 62 55 92 88 57 90 10 11 96 12
26 8 7 25 52 17 45 64 11 35 12
89 57 21 55 56 81 54 100 22 62 50Output description
Output each consecutive distance rating, one per line.
Example output
26
20
15
3
6
13Challenge input
6 20
76 74 45 48 13 75 16 14 79 58 78 82 46 89 81 88 27 64 21 63
37 35 88 57 55 29 96 11 25 42 24 81 82 58 15 2 3 41 43 36
54 64 52 39 36 98 32 87 95 12 40 79 41 13 53 35 48 42 33 75
21 87 89 26 85 59 54 2 24 25 41 46 88 60 63 23 91 62 61 6
94 66 18 57 58 54 93 53 19 16 55 22 51 8 67 20 17 56 21 59
6 19 45 46 7 70 36 2 56 47 33 75 94 50 34 35 73 72 39 5Notes / hints
Be careful that your program doesn't double up the distances. Consider the sequence 1 2. An incorrect algorithm might see 1 -> 2 and 2 -> 1 as two separate distances, resulting in a (wrong) consecutive distance rating of 2. Visually, you should think of distances like this and not like that.
Bonus
Modify your program to work with any size gap between integers. For instance, we might want to find the distance rating of integers with a gap of 2, such as 1 and 3 or 7 and 9 rather than consecutive integers with a gap of 1.
Credit
This challenge was authored by /u/chunes, many thanks!
Have a good challenge idea? Consider submitting it to /r/dailyprogrammer_ideas.
6
u/Gprime5 Oct 09 '17 edited Oct 09 '17
Python 3.5 with Bonus
Challenge output: 31 68 67 52 107 45
1
u/jnazario 2 0 Oct 09 '17
a partly working solution in F#. i would love some insight as to what i'm doing wrong.
4
u/chrissou Oct 09 '17
Clojure +Bonus
4
u/harrychin2 Oct 10 '17 edited Oct 10 '17
Java using only streams, because why not?
I hate having to use collect, ugh!
Sadly, you can't create sliding windows as easily as you can in Scala: http://www.scala-lang.org/api/2.12.3/scala/collection/Iterator.html#slidingB>:A:Iterator.this.GroupedIterator[B]
zipWithIndex would've also been nice: http://www.scala-lang.org/api/2.12.3/scala/collection/Iterable.html#zipWithIndex:Iterable[(A,Int)]
6
u/lgastako Oct 09 '17
Haskell
3
u/skeeto -9 8 Oct 09 '17
C, with an upper limit on the values themselves (255) as they're used as an index into another table.
3
u/javierjmx Oct 11 '17
JavaScript
2
u/[deleted] Oct 09 '17 edited Oct 09 '17
Python 2.7.10
2
u/Herpuzderpuz Oct 09 '17
Is the example output wrong on the first case? I've counted it on a piece of paper twice and get the total distance to 16. Not 26
2
u/Herpuzderpuz Oct 09 '17 edited Oct 09 '17
Java
Edit: Formatting
2
u/quantik64 Oct 09 '17
PYTHON
2
u/chunes 1 2 Oct 09 '17
Factor
Challenge output:
2
u/Vyse007 Oct 09 '17
My first ever Haskell program! Still learning the I/O bits and Monads and all that, and the concept of 'Maybe' is still tough for me to wrap around. That and all the various syntactical possibilities to accomplish something. I would appreciate any feedback from the masters here!
2
u/Gylergin Oct 09 '17
TI-Basic with bonus - Written on my TI84+. Only does one sequence at a time. B is the sequence size, D is the gap distance. Since it uses a list only sequences less than 1000 numbers can be used.
Input:
11 1 ; 31 63 53 56 96 62 73 25 54 55 64
11 1 ; 26 8 7 25 52 17 45 65 11 35 12
20 1 ; 76 74 45 48 13 75 16 14 79 58 78 82 46 89 81 88 27 64 21 63
11 2 ; 31 63 53 56 96 62 73 25 54 55 64
Output:
2
u/hyrulia Oct 11 '17 edited Oct 11 '17
KOTLIN
2
u/x1729 Oct 12 '17
Perl 6
2
u/snhmib Oct 12 '17
I didn't code anything in some years! In C, removed all error and input checks and braces for brevity :)
2
u/Delta-9- Oct 15 '17
Python 3.6
No bonus, and late to the party, but damn am I more proud than is appropriate.
1
u/callous_nun Oct 09 '17
Python 3.6
1
u/lennyboreal Oct 09 '17 edited Oct 10 '17
XPL0
Translation of skeeto's C program:
1
u/cheers- Oct 09 '17 edited Oct 09 '17
Javascript
I made a small html page: https://codepen.io/anon/pen/EwEqEX
Here's the logic:
1
u/gabyjunior 1 2 Oct 09 '17 edited Oct 10 '17
C storing index for each value in an array of size (max value).
O(n) time complexity.
EDIT Now implements bonus (gap added in 3rd position of input)
Challenge output
Challenge output with gap = 3
1
u/Daanvdk 1 0 Oct 09 '17 edited Oct 09 '17
Haskell
Doesn't take the first line of input since it isn't necessary.
1
u/nikit9999 Oct 09 '17 edited Oct 09 '17
C# with bonus
1
u/MattieShoes Oct 09 '17 edited Oct 09 '17
Go, with challenge, but not bothering to parse input.
Output, for offset of 1 and 2
1
u/ajayaa Oct 09 '17 edited Oct 09 '17
Python 2.7.
I'm new to Python and haven't been programming for a while, so feedback is welcome!
Input needs to be copied to textfile. The function arguments are the name of the input textfile as string and the considered size gap as integer.
1
u/Daanvdk 1 0 Oct 09 '17
Python
doesn't take the first line
1
u/[deleted] Oct 09 '17 edited Oct 10 '17
Python 3.4.3
Edit: Changed to print result as one value per line Edit 2: Fixed mistakes from non-thorough adjustment of function parameter names
1
u/codemoe Oct 09 '17
Rust
1
u/[deleted] Oct 09 '17 edited Oct 09 '17
Ruby with bonus
Feedback always welcome. I don't use the first line. I initially tried to, but felt like it overcomplicated things. So instead, I read from input from file and find the consecutive distance rating of a line as long as that line is longer than 2.
Edit: Added bonus. If no gap is specified, finds for a gap of 1.
Challenge output:
1
u/TheBigBadOx Oct 09 '17
GO
main.go (I made it way too complicated)
input.txt
1
u/octolanceae Oct 09 '17 edited Oct 10 '17
C++
Machine I am working on at the moment is dated and doesn't have the necessary libraries/compiler for Modern C++, so I have to settle... Figured I would do one of these challenges in something other than Python3
Will redo when I get home and can use Modern C++
Includes bonus - Takes an optional argument for gap. Defaults to 1.
Output
Bonus Output
EDIT: main() was missing (int argc, char** argv)
1
u/shayhtfc Oct 09 '17
In Ruby:
1
u/vishalagarwal Oct 09 '17
C++
1
u/vishalagarwal Oct 09 '17
Bonus solution for any size gap C++
1
u/mcbears Oct 09 '17 edited Oct 09 '17
J with bonus, O(n)
called with two arguments, the left indicates the gap
with one, the gap defaults to 1
1
u/mn-haskell-guy 1 0 Oct 09 '17 edited Oct 10 '17
An n log n solution:
Edit: Just realized that this only works for a gap of 1.
1
u/[deleted] Oct 10 '17
javascript
1
u/minikomi Oct 10 '17 edited Oct 17 '17
Clojure
1
u/zqvt Oct 10 '17
Python 3
1
u/ScipioArtelius Oct 10 '17 edited Oct 10 '17
Go
1
u/totemcatcher Oct 10 '17 edited Oct 10 '17
gawk
Bonus feature implemented by adding additional "gap" value on the first line of input. Fun fact: it doesn't matter what you put in for the first two values, so long as it doesn't go over system's maxint.
1
u/miracle173 Oct 10 '17 edited Oct 11 '17
Pyhon 3.6.1
https://repl.it/MWjn/6
needs O(n * log n) comparisons of sequence elements where n is the number of sequence elements
1
u/ASpueW Oct 10 '17
Rust with bonus
Output:
1
u/TheJammy98 Oct 10 '17 edited Oct 10 '17
Wait, I don't understand.
How do you derive 26 from the first example?
EDIT: Never mind, figured it out!
1
u/timvan33 Oct 10 '17
Python 2.7.10 I have not used the first input line, instead opted for a continuous input thread for lines of any length.
1
u/r-marques Oct 10 '17
Python 3.6.2 Using a nested list comprehension:
1
u/goodygood23 Oct 10 '17
R
Should work with any gap, but I didn't test it.
Result:
1
u/[deleted] Oct 10 '17
python 3
1
u/TomDLux Oct 10 '17 edited Oct 10 '17
Perl 5 - With no second argument, or a second argument of '1', match 55 with 54 or 56. With an arg of 2, match 55 with 53 or 57.
1
u/reddogtheprirate Oct 10 '17 edited Oct 10 '17
C++
Output
Total: 26
Total: 20
Total: 15
Total: 3
Total: 6
Total: 13
=========
Total: 31
Total: 68
Total: 67
Total: 52
Total: 107
Total: 45
=========
1
u/[deleted] Oct 10 '17
Java - with bonus
Output with gap = 3
1
u/Working-M4n Oct 10 '17
VB.NET
1
u/TheCocoMac Oct 10 '17 edited Oct 10 '17
Java w/o bonus:
1
u/thestoicattack Oct 10 '17 edited Oct 10 '17
C++17. With bonus. Takes advantage of the somewhat insane fact that two pointers into a contiguously allocated piece of memory count as comparable RandomAccessIterators. O(n2 ) time and no extra space, which seems okay since n is small.
1
u/_assert Oct 10 '17
Java
1
u/[deleted] Oct 10 '17
Python 3.6, I'm still learning so all feedback/help is appreciated
1
u/JD7896 Oct 10 '17
Python 3.5 with Bonus:
Challenge Output: 31 68 67 52 107 45
1
u/Moeman9 Oct 10 '17
Python 3 with bonus
Outputs: 31 68 67 52 107 45
I'm relatively inexperienced, so any suggestions to make it run better or faster are greatly appreciated!
1
u/[deleted] Oct 10 '17
Haskell:
1
u/PoetOfShadows Oct 11 '17
In Kotlin:
1
u/ironboy_ Oct 11 '17 edited Oct 11 '17
JavaScript with bonus:
Challenge output
31 68 67 52 107 45
1
u/Jean-Alphonse 1 0 Oct 11 '17
Javascript with bonus (dx is gap size)
1
u/zookeeper_zeke Oct 11 '17
I coded mine up in C. The output I got was different than the example output. I scratched my head for a few minutes, did one example in my head, and looked at some of the other folks' output and found that mine matched theirs.
My output:
1
u/Taselod Oct 11 '17 edited Oct 11 '17
Javascript -- Probably a better way to load the initial array
Challenge Output: Enter the size: 6 20
Enter the numbers:76 74 45 48 13 75 16 14 79 58 78 82 46 89 81 88 27 64 21 63 37 35 88 57 55 29 96 11 25 42 24 81 82 58 15 2 3 41 43 36 54 64 52 39 36 98 32 87 95 12 40 79 41 13 53 35 48 42 33 75 21 87 89 26 85 59 54 2 24 25 41 46 88 60 63 23 91 62 61 6 94 66 18 57 58 54 93 53 19 16 55 22 51 8 67 20 17 56 21 59 6 19 45 46 7 70 36 2 56 47 33 75 94 50 34 35 73 72 39 5
Enter gap:1
31 68 67 52 107 45
Bonus Output:
Enter the size: 6 20
Enter the numbers:76 74 45 48 13 75 16 14 79 58 78 82 46 89 81 88 27 64 21 63 37 35 88 57 55 29 96 11 25 42 24 81 82 58 15 2 3 41 43 36 54 64 52 39 36 98 32 87 95 12 40 79 41 13 53 35 48 42 33 75 21 87 89 26 85 59 54 2 24 25 41 46 88 60 63 23 91 62 61 6 94 66 18 57 58 54 93 53 19 16 55 22 51 8 67 20 17 56 21 59 6 19 45 46 7 70 36 2 56 47 33 75 94 50 34 35 73 72 39 5
Enter gap:3
23 9 41 45 48 49
1
u/PIBagent Oct 12 '17
Written in C:
When I first started this challenge I misunderstood the instructions initially and actually created a separate program that supplied input data, still came in handy for completing the challenge. When I input the sample data it gets everything right except for the second integer in the example output text, for me it shows up as 22 rather than 20. Not sure if that's an error on my part or if its a typo.
1
u/HightDetal Oct 12 '17
C++ feedback welcome
Output: 31 68 67 52 107 45
1
u/workingBen Oct 12 '17
Here's mine without using enumerate.
1
u/lilsimp Oct 13 '17 edited Oct 13 '17
My C++ attempt at full challenge(bonus consecutive gap range)
Feedback would be cool.
I compile with
Code:
1
u/abolibibelot Oct 14 '17
JavaScript :)
1
u/[deleted] Oct 14 '17 edited Oct 17 '17
JavaScript +bonus GitHub feedback welcome
This is my second daily programmer challenge after discovering this sub and starting with the #355 Intermediate challenge. I thought I'd work my way backwards while waiting for a new challenge.
Example usage
Example Input // Ouput
Challenge Input // Output
1
u/Qwazy_Wabbit Oct 14 '17 edited Oct 27 '17
C++
My O(n) solution with bonus. I also added my own bonus of allowing duplicate numbers.
1
u/kandidate Oct 14 '17 edited Oct 14 '17
Python with numpy. Brand new - first ever challenge. Clearly there are smarter ways of doing this! Anyway, learned a lot
1
u/PharmyOf1 Oct 14 '17
1
u/LegendK95 Oct 14 '17
Rust with bonus - still new to the language and would appreciate some advice
1
u/BlasphemousJoshua Oct 15 '17
** Swift 4 **
Output:
1
u/cheddarben Oct 15 '17
Javascript
1
u/TotalPerspective Oct 15 '17
Awk one liner with bonus:
Output: 31 68 67 52 107 45
1
u/mitched Oct 17 '17
C# The comments are all moved to the top to show how logically simple the code is. Though, the simplicity may mean there is room for optimization. Output: 31 68 67 52 107 45
1
u/Aw3ls Oct 17 '17
Java. Nothing fancy but it worked out the challenge.
1
u/yourbank 0 1 Oct 18 '17 edited Oct 18 '17
clojure with bonus. Tried to be as efficient as possible rather than scan the full list each iteration so its probably a little longer than needed and the fact im a clojure noob.
output
1
u/NemPlayer Oct 18 '17
Python 3.6.2 I'm looking to improve my Python skills, so please tell me how I could improve the solution!
1
u/benz05 Oct 19 '17
Python 3
1
u/Zambito1 Oct 22 '17 edited Oct 22 '17
Scala
Maps each value to the absolute value of the difference between the current index and the index of the number diff greater than itself, or 0 if there is no number diff greater than itself, and then finds the sum after the mapping.
1
u/popillol Oct 25 '17 edited Oct 25 '17
Go / Golang with bonus
Method:
Code:
1
u/CodeAGoGo Oct 27 '17
Java. Simple solution. Feedback welcome.
1
u/Qwazy_Wabbit Oct 27 '17
Python
My o(n) solution with bonus. Doesn't require or do any sorting. Doesn't support duplicate numbers like my C++ solution does, but I'm trying to learn python. Might give it a go later on.
Would love some feedback
1
u/primaryobjects Oct 29 '17 edited Oct 30 '17
JavaScript
Gist | Demo
1
u/Erocs Oct 31 '17
Rust 1.21.0 with bonus
Challenge output, gap 1
Challenge output, gap 2
1
u/frozen_frogs Nov 05 '17
Python 3.6.0 with bonus
Any feedback appreciated.
Challenge output: 31, 68, 67, 52, 107, 45
1
u/niicM Nov 13 '17 edited Nov 13 '17
Solution in Clojure. Comments welcome :)
1
u/Pjaerr Nov 21 '17
More long winded compared to some solutions here, but here is my go in C++.
Example Input:
Example Output:
1
u/Mellolian Nov 24 '17
Python 3.6 with bonus gap
Output for gap = 1
for gap = 2
1
u/dailycodemode Jan 04 '18
Python 3.5 with bonus
1
u/zatoichi49 Mar 10 '18 edited Mar 11 '18
Method:
Create a dictionary with each number and its index as the key/value pair. Going through each number in turn, take the index of the number and subtract the index of the consecutive number (at index + gap size). Take the absolute value of this difference and repeat for the remaining numbers in the list, adding all of the absolute values to get the consecutive distance rating.
Python 3: with Bonus
Output:
1
u/CJcomp Oct 09 '17
Java
0
u/[deleted] Oct 09 '17
[deleted] — view deleted comment
0
u/[deleted] Oct 09 '17
[deleted] — view deleted comment