r/dailyprogrammer • u/Coder_d00d 1 3 • Aug 13 '14
[8/13/2014] Challenge #175 [Intermediate] Largest Word from Characters
Description:
Given a string of words and a string of letters. Find the largest string(s) that are in the 1st string of words that can be formed from the letters in the 2nd string.
- Letters can be only used once. So if the string has "a b c" then words like "aaa" and "bbb" do not work because there is only 1 "a" or "b" to be used.
- If you have tie for the longest strings then output all the possible strings.
- If you find no words at all then output "No Words Found"
input:
(String of words)
(String of characters)
example:
abc cca aaaaaa bca
a b c
output:
List of max size words in the first string of words. If none are found "No Words Found" displayed.
example (using above input):
abc bca
Challenge input 1:
hello yyyyyyy yzyzyzyzyzyz mellow well yo kellow lellow abcdefhijkl hi is yellow just here to add strings fellow lellow llleow
l e l o h m f y z a b w
Challenge input 2:
sad das day mad den foot ball down touch pass play
z a d f o n
Got an Idea For a Challenge?
Visit /r/dailyprogrammer_ideas and submit your idea.
57
Upvotes
6
u/threeifbywhiskey 0 1 Aug 13 '14 edited Aug 13 '14
Ruby:
6
u/NewbornMuse Aug 13 '14
Python: A couple list comprehensions do the trick. I'm very open to criticism.
11
u/[deleted] Aug 13 '14 edited Aug 13 '14
Six lines of Python 3 with re and sorting.
Credits to http://codegolf.stackexchange.com/a/5546
Line by line explanation:
9
u/skeeto -9 8 Aug 13 '14 edited Aug 13 '14
C. To check for a match it sorts the letters, then recursively matches up characters (
is_subseq). For simplicity, the maximum number of words and letters is hardcoded.4
u/XenophonOfAthens 2 1 Aug 13 '14
In Prolog, short and sweet:
This is only the logic, though, because I have no idea how to read from standard input :) You have to run this of the interpreter command line.
4
u/VerilyAMonkey Aug 13 '14
Python 2.7. Nearly cheating because standard library.
Challenge 1:
Challenge 2:
3
u/xiongy Aug 13 '14 edited Aug 13 '14
Here's my naive Python implementation:
Feedback appreciated.
3
u/nuunien Aug 14 '14
In Go, might be a bit large, should be UTF-8 compatible, improvements welcome!
2
u/wadehn Aug 13 '14 edited Aug 13 '14
C++: Straightforward use of a
std::unordered_multiset<char>to determine whether a given word can be composed from the given multiset of characters.Challenge output 1:
Challenge output 2:
2
u/Coplate Aug 13 '14
Real quick perl program based on counting the number of each letter in the word, and comparing it to the number available.
2
u/marchelzo Aug 13 '14
Not a very interesting approach here:
2
u/mebob85 Aug 13 '14 edited Aug 13 '14
C++ solution, using a table of character counts to quickly check if a given string is composed of the given characters:
If I did the math correctly, this runs in O(n log n + w) worst case time, where n is the number of words and w is the total length of all words.
2
u/Quadrophenic Aug 13 '14 edited Aug 13 '14
PHP (yes, blah blah blah PHP is terrible). Pretty straightforward, use a hashmap to track letter counts for each word.
Runs in O(w + l), where w is the total length of all words and l is the number of letters. I see a potential tradeoff where we could sort by word-length descending, and then break out of our loop early once we've found a word and we reach something of shorter length, but this is highly input dependent on whether or not that will help and its worst case is worse.
I would love feedback! Thanks.
2
u/silver67 Aug 13 '14
Python 2.7.2 Any feedback welcome
Output
2
u/Zegrento7 Aug 13 '14
Learnin myself a Haskell for great good!
I'm very new to this, so sorry if it burns your eyes.
2
u/lukz 2 0 Aug 13 '14
vbscript,
where you obviously don't have sets, sorting, or list filtering, only plain arrays.
2
u/hutsboR 3 0 Aug 13 '14 edited Aug 13 '14
Dart:
You can't directly access the chars of a string in Dart, so you have to jump around from code unit to string and vice versa. It adds some extra noise.
Usage:
2
u/99AFCC Aug 13 '14
Quickly written Python 2.7. Not the most efficient.
Test cases:
Output:
2
u/brynnflynn Aug 14 '14 edited Aug 14 '14
This was a fun one. I started out using a list, but realized a dictionary would be much faster. C#
2
u/lucaswerkmeister Aug 14 '14 edited Aug 14 '14
Ceylon:
I check if a word is okay by comparing the count of each character in the word with the count of that character in the list of characters.
(I know it’s inefficient – I count characters that appear in the word repeatedly multiple times; I could store the counts in the list of characters, since they don’t change; and I could also only check words that are the same length or longer. I don’t care, I didn’t want to make this too complicated.)
Note: This is for Ceylon 1.1, which hasn’t yet been released. Here’s a version that works with the Ceylon Web Runner (Ceylon 1.0, no
runfunction wrapper, noLinkedList).2
u/tally_in_da_houise Aug 14 '14
Python 2.7+ class implementation.
Usage:
Code:
2
u/brugaltheelder Aug 15 '14
Python code using Counter for non-unique list intersection.
Output:
2
u/slwz Aug 15 '14
I may be late to the party, but here have some pie. Python, I mean.
3
u/13467 1 1 Aug 13 '14 edited Aug 14 '14
3
u/[deleted] Aug 13 '14
Done in Java
Output
1
u/kirsybuu 0 1 Aug 13 '14 edited Aug 13 '14
D Language:
Challenge Input:
1
u/cohen_dev Aug 14 '14
Python.
1
u/VerifiedMyEmail Aug 14 '14 edited Aug 14 '14
python 3.3
1
u/zifu Aug 14 '14 edited Aug 14 '14
Javascript!
Iterates through each input word's characters and removes it if it finds characters that aren't in the 'filter' input. O( N2 ), during this process also tracks which word is the longest, or longests, then removes others from results array and prints to web page.
Live version!
Code!
1
u/stabzorzz Aug 14 '14
Done in python 2.7. Feedback is welcome. Code seems pretty streamlined but if you see anything that can be better feel free to post a comment.
1
u/minikomi Aug 14 '14 edited Aug 14 '14
My first haskell solution!
1
u/CatatonicMan Aug 14 '14
Python 2.7. I sorted the words by length, then tested each one largest to smallest until I found all matches of a given word length. The helper checks if a word can be spelled with the given letters.
Testing code.
1
u/ff123 Aug 14 '14
Lua. Not the most elegant solution, but it works. Not having a continue keyword annoyed me for a while.
1
u/Edward_H Aug 14 '14
104 lines of COBOL:
1
u/fvandepitte 0 0 Aug 14 '14
C#
Usage:
words.txt:
Result:
Code:
1
u/basic_bgnr Aug 14 '14
Python:
1
u/bcd87 Aug 14 '14
Longest entry?
1
u/fbgm1337 Aug 14 '14
Swift. Uses dictionaries to get character counts.
1
u/joyeuxnoelle Aug 14 '14 edited Aug 14 '14
Python 3.4. Not as Pythonic as it could be; I'm still getting the hang of things. I'd love to say "I wanted to do it without lambdas", but really I just forgot you could.
1
u/Hazzaman99 Aug 15 '14
Python 2.7.5
1
u/Toans Aug 15 '14
My first Haskell solution. Filters away the invalid words, sorts them by length in descending order, groups them by length and returns the first element of the resulting list of lists.
1
u/thinksInCode Aug 15 '14
Groovy. Any criticism is welcome - I'm still learning Groovy.
1
u/kriskova Aug 16 '14
Ruby. Feel free to comment. (newbie here)
1
u/[deleted] Aug 16 '14
Python 2.7. Feedback/critiques are welcomed.
1
u/HackSawJimDuggan69 Aug 16 '14
By no means the best Python solution but this is my first intermediate problem. I'm looking over the other Python solutions now, but any particular advice would be appreciated. :)
1
u/ddsnowboard Aug 16 '14
Been playing with Java lately because I need to brush up on it for the upcoming robotics season. Here's my attempt. It's a little lengthy, and I could probably save some lines here and there, but it works. Any feedback is welcome. Also, it takes the input in a text file.
1
u/Reverse_Skydiver 1 0 Aug 17 '14
Guess I could say it's been obfuscated:
1
u/sagan_radiation Aug 17 '14 edited Aug 17 '14
Python 3.4
1
u/blaine64 Aug 18 '14 edited Aug 18 '14
Feedback on coding style encouraged!
JavaScript:
1
u/GambitGamer Aug 18 '14
I'm very much a beginner so feedback is appreciated!
Python 3.4.1:
1
u/anserk Aug 18 '14
Python:
Output:
1
u/[deleted] Aug 18 '14
Here's my C# code:
}
1
u/killmefirst Aug 19 '14
C++ using stringstreams:
That gives me
1
u/dznqbit Aug 20 '14
Swift. Works with Unicode characters! :) Try this for input:
おもしろい おもでとう ありがとう おまつり ちがいです
お ま り つ
1
u/p_mathew Aug 21 '14
Java :
package Test;
import java.util.ArrayList; import java.util.ListIterator;
public class LargeWord {
}
1
u/jppunnett Aug 22 '14
In Python v 3.4.1
1
u/Graut Sep 23 '14
1
u/ENoether Aug 13 '14
Python 3.4.1, with challenge inputs. As always, feedback and criticism welcome:
Output:
1
u/MaximaxII Aug 13 '14 edited Aug 13 '14
There we go! Feedback and criticism are welcome, as always :)
Challenge #175 Intermediate - Python 3.4
Output
Challenge input 1:
Challenge Input 2:
Edit: To the guy who downvoted me, could I get feedback instead of a downvote? This is the second time that I am being downvoted for no reason, probably because someone wants to get their own comment higher :(
1
u/nothingtofollow Aug 13 '14 edited Aug 13 '14
Python 2.7 using Counter class
1
u/Godspiral 3 3 Aug 13 '14 edited Aug 13 '14
in J,
;: inv 'abc' maxlenitems@:anamatch 'abc cca dabc aaaaaa bca dab'
abc bca
to get all matches:
;: inv 'dabc' anamatch 'abc cca dabc aaaaaa bca dab'
abc dabc bca dab
spoiler
1
u/dohaqatar7 1 1 Aug 13 '14 edited Aug 13 '14
Some beautiful Haskell:
1
u/[deleted] Aug 13 '14 edited Aug 13 '14
Java, takes a command-line arg, assumes you wrapped them in quotes.
I don't really understand it entirely. For an example, if I feed:
"aaa abc bca bbb"
"a b"
Should I print "ab" or "No Words Found" ?
I'm assuming the first, and I'm matching partial words.
If the latter, modify that if block in my outer loop to
Output 1:
Output 2:
Output 2 using the modified if block:
0
u/[deleted] Aug 13 '14
[deleted] — view deleted comment
3
u/[deleted] Aug 13 '14
[removed] — view removed comment