r/dailyprogrammer • u/rya11111 3 1 • May 09 '12
[5/9/2012] Challenge #50 [easy]
Hello everyone! As of today, we have finished our 50th challenge and it has been a pleasure giving out these challenges to you all. You have all been amazing with the solutions and seeing you all i hope i become a good programmer like you all one day :D
If i did any mistakes in challenges please forgive me and as you may have noticed we post once in two days or so to give you time to complete these. Really sorry if you wanted everyday posts .. but due to our busy lives, maybe sometime in future or maybe when i leave this subreddit, you may have that in the new management :) Thank You one and all ... As for now I have given today's two challenges are from Google Code Jam Qualification Round Africa 2010
Store Credit:
You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solution you provide will consist of the two integers indicating the positions of the items in your list (smaller number first).
For instance, with C=100 and L={5,75,25} the solution is 2,3; with C=200 and L={150,24,79,50,88,345,3} the solution is 1,4; and with C=8 and L={2,1,9,4,4,56,90,3} the solution is 4,5.
PROBLEM A IN THE LINK. PLEASE USE IT TO CLARIFY YOUR DOUBTS
P.S: Special Thanks to the other moderators too for helping out :)
3
u/SwimmingPastaDevil 0 0 May 09 '12
Python: probably a very un-pythonic code, but i am just learning python:
3
u/huck_cussler 0 0 May 10 '12
2
u/[deleted] May 09 '12
Ruby.
2
u/ixid 0 0 May 09 '12 edited May 09 '12
D
2
u/[deleted] May 09 '12
Java, returns first match found.
2
u/luxgladius 0 0 May 09 '12
Perl, runs in linear time
Output
100 5 75 25: 2 3
200 150 24 79 50 88 345 3: 1 4
8 2 1 9 4 4 56 90 3: 4 5
2
u/r4n May 10 '12
Java, surely it can be done better, so any feedback will be apreciated.
2
u/jonzo1 0 0 May 10 '12
In Clojure:
Maybe not perfect, but I'm still learning.
1
u/Ttl May 09 '12
Inefficient solution in Python. Returns all solutions:
1
u/emcoffey3 0 0 May 09 '12
Using C# and LINQ:
1
u/alderz May 10 '12
Python:
1
u/prophile May 10 '12
1
u/crawphish May 10 '12
Java:
1
u/MusicalWatermelon May 10 '12
I don't know how to post code, or how to do the spoiler-thingy (please explain) so here Java in pastebin
http://pastebin.com/X2122F6x
probably not the best code, but for knowing java for 4 months, not bad I think ;)
1
u/mwc42 May 11 '12 edited May 11 '12
PHP: I think it works for any list of prices.
1
u/Devanon May 14 '12
My linear-time solution in Ruby:
1
u/Sturmi12 May 14 '12
Java:
Value I is never used. meh.
1
u/Medicalizawhat May 25 '12
Doesn't look like anyone posted a C solution:
1
u/bigronaldo May 30 '12
C#
1
u/[deleted] Oct 06 '12 edited Oct 06 '12
JavaScript
ch50(8,[2,1,9,4,4,56,90,3]); // output: 4,5
some of us only returns exact matches. i dont know why. this one (sadly O(n*n)) iterates over all the values and catches all the smaller and smaller gaps between the two numbers and then outputs it or even find the exact match.