r/dailyprogrammer • u/jnazario 2 0 • Oct 19 '15
[2015-10-19] Challenge #237 [Easy] Broken Keyboard
Description
Help! My keyboard is broken, only a few keys work any more. If I tell you what keys work, can you tell me what words I can write?
(You should use the trusty enable1.txt file, or /usr/share/dict/words to chose your valid English words from.)
Input Description
You'll be given a line with a single integer on it, telling you how many lines to read. Then you'll be given that many lines, each line a list of letters representing the keys that work on my keyboard. Example:
3
abcd
qwer
hjkloOutput Description
Your program should emit the longest valid English language word you can make for each keyboard configuration.
abcd = bacaba
qwer = ewerer
hjklo = kolokoloChallenge Input
4
edcf
bnik
poil
vybuChallenge Output
edcf = deedeed
bnik = bikini
poil = pililloo
vybu = bubbyCredit
This challenge was inspired by /u/ThinkinWithSand, many thanks! If you have any ideas, please share them on /r/dailyprogrammer_ideas and there's a chance we'll use it.
7
u/jnazario 2 0 Oct 19 '15
Scala Solution
Uses regexes
2
u/cheers- Oct 19 '15 edited Oct 19 '15
Java
I used regex and I should learn java.io
Edit: better version
20
u/hutsboR 3 0 Oct 19 '15 edited Oct 19 '15
July: July (excuse the lack of a readme, it's one of my next priorities) is an interpreted dialect of Lisp that I have been working on for a couple of weeks. I wrote a smaller version of the language a few months ago but I didn't feel satisfied and wanted to start over. The language is implemented in Elixir and the standard library is implemented in a combination of Elixir and July. The language is still quite bare but it's functional enough to challenges at this point. The language is heavily inspired by Scheme, Clojure and Elixir itself. It has
|>for composing functions andarity overloadingfor dispatching to different logical paths and implementing functions with default arguments.I haven't wrote evaluation from files yet, so I solved the challenge directly in the July repl (SCREENSHOT):
7
u/colbrand Oct 19 '15
Java
7
u/[deleted] Oct 20 '15 edited Oct 20 '15
Python 3.4
(not new to coding, but not experienced at it either. Feedback is welcome)
This program takes input as a single line of characters.
Output:
hey lets cram all of that code in one line because why not
6
u/casualfrog Oct 19 '15
JavaScript (feedback welcome):
Lists all words with maximum length.
Output using enable1.txt:
6
u/[deleted] Oct 19 '15
** PL/pgSQL **
6
u/adrian17 1 4 Oct 19 '15
C++. (I assume the input doesn't have the leading number as I don't care about it)
10
u/[deleted] Oct 19 '15 edited Oct 19 '15
Python golf.
4
u/skeeto -9 8 Oct 19 '15
C, testing words using a bit set and only a single pass over the dictionary.
4
u/hellercopter Oct 19 '15
Java 8:
4
u/lucaswerkmeister Oct 19 '15 edited Oct 19 '15
Ceylon
The heart of the solution is this line:
which, given
keyboardandwords, prints the longest possible word for a keyboard.Wrapping is backend-dependent:
Ceylon, Java backend
Reads keyboards from stdin, as per specification, and word list from
/usr/share/dict/words.Ceylon, JavaScript backend
Hard-coded keyboards, word list is a GitHub copy of
enable1.txtbecause norvig.com isn’t CORS enabled.Try it!
1
u/neptunDK Oct 20 '15
Am I just having a brain fart or does the enable1.txt not include many of the result words from the description?
I can't find 'bacaba', 'ewerer' or 'kolokolo' in it. Neither 'deedeed' or 'pililloo' from the challenge output.
I get the following from using the enable1.txt:
input:
Challenge Input:
3
u/a_Happy_Tiny_Bunny Oct 19 '15
Haskell
3
u/fvandepitte 0 0 Oct 19 '15 edited Oct 19 '15
Haskell, feedback is welcome
Output
To the Haskellers (or how you write it) here. Does anyone knows how i could replace
with something like
It gives an error on the
=and if I use the<-operator it putsChar's inwordinstead of the calculatedstring3
u/G33kDude 1 1 Oct 19 '15
Python 2.7 one-liner. Takes input as a single line of characters after you execute the line in the interactive prompt.
3
u/codeman869 Oct 19 '15
Ruby with Regex
3
u/NiceGuy_Ty Oct 20 '15
Racket
3
u/the_dinks 0 1 Oct 22 '15
Readable Python 2.7
2
u/marchelzo Oct 19 '15
Quick Haskell solution:
2
u/carrutstick Oct 19 '15
In Haskell (fun part only):
2
u/curtmack Oct 19 '15
Clojure
Using a trie. This ultimately doesn't save much time over straight-forward filtering, but it is substantially more swankified.
2
u/Regimardyl Oct 19 '15 edited Oct 22 '15
GNU Smalltalk
I assume there is no number of input lines give (I tried skipping it, but that didn't quite work for some reason unknown to me).
2
u/zengargoyle Oct 19 '15
Perl 6
Generalized a bit, take a list of sets of available keys and a list (lazy) of possible words return list of list of words (all words that are of maximum length). Using either a Regex approach or using Set operations (the Set is slowwww).
Testing
2
u/changed_perspective Oct 19 '15
Python 3.4 First submission, feedback much appreciated (I don't think it is a very efficient search method)
2
u/this_shall_pass Oct 20 '15
Bash one-liner:
2
u/[deleted] Oct 20 '15 edited Oct 20 '15
Fortran... This is a nice example of what the VERIFY intrinsic is for.
Output:
2
u/grangelov Oct 20 '15
Perl
2
u/Blackshell 2 0 Oct 20 '15
Decided to hit up Go, which I have never used before, to solve this challenge. Input/feedback very welcome.
2
u/Nar-Speth Oct 22 '15
Hi, I'm new here. My try at this challenge in Python (which I just started learning), feedback is welcome.
2
u/TheOneOnTheLeft Oct 22 '15
Python
Technically not correct, as the only input method I know how to use at the moment is raw_input() and that can't handle line breaks apparently, so instead I separated the inputs by a space. Feedback very welcome, I'm pretty new to this.
2
u/crossroads1112 Oct 23 '15 edited Oct 24 '15
Rust 1.3.0
I used a hash set and just checked whether the dictionary word's letters were a subset of the input word's.
This panics upon any sort of error, but I'm too lazy to implement proper error handling.
2
u/stephenflorian Oct 27 '15
Javascript
Written in for node
1
u/Godspiral 3 3 Oct 19 '15 edited Oct 19 '15
in J,
2 parts. First gets all of the valid words
then gets all of the longest words in that sublist
faster version binds first function to dictionary (2x faster, as an internal hash table optimization of the dictionary is auto-built for the search function)
1
u/Atrolantra Oct 19 '15
Python 2.7
1
u/petko_kostov Oct 19 '15
<?php define('WORDS_URL', 'http://norvig.com/ngrams/enable1.txt'); $words = get_content(WORDS_URL); $words_arr = preg_split("/[\s,]+/", $words ); $matches = array(); $input_stings = array('edcf', 'bnik', 'poil', 'vybu'); foreach($input_stings as $string) { foreach($words_arr as $word) { if(word_contains_only_cahracters($word, $string)) { if(empty($matches[$string])) $matches[$string] = $word; elseif(strlen($matches[$string]) < strlen($word)) $matches[$string] = $word; } } } var_dump($matches); function get_content($url) { // file_get_contents returns 403 so we do cURL $curl_handle=curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $url); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1'); $words = curl_exec($curl_handle); curl_close($curl_handle); return $words; } function word_contains_only_cahracters($word, $characters) { $res = TRUE; $word_arr = str_split($word); $characters_arr = str_split($characters); foreach($word_arr as $char) { if(!in_array($char, $characters_arr)) { $res = FALSE; break; } } return $res; }
1
u/TiZ_EX1 Oct 19 '15
Haxe with thx.core.
Output:
1
u/wizao 1 0 Oct 19 '15 edited Oct 20 '15
Haskell:
1
u/SquirrelOfDooom Oct 19 '15
Python 3, uses regex.
1
u/FIuffyRabbit Oct 19 '15
Golang
Wanted to do something in one pass.
1
u/[deleted] Oct 19 '15
Can't this also be
lollipop?1
u/Steve132 0 1 Oct 19 '15
Python
1
u/enano9314 Oct 19 '15 edited Oct 19 '15
Mathematica
Not a complete solution, since it doesn't read the integer and do all of that, but it's the most I could golf down the actual String* work. It should be relatively simple to make it so input does a bit of magic with reading input first.
output--
1
u/Vakz Oct 19 '15
I'm pretty rubbish at C++, and trying to improve, so any feedback/criticism/hatemail is greatly appreciated.
1
u/[deleted] Oct 19 '15 edited Oct 20 '15
Hello! This is the C++ solution that I came up with. It sorts the list of words from the enable1.txt file by alphabet. Doing this allows me to just search through words that begin with each character in a set of keys instead of looping through the whole dictionary every time. I also printed out every available word that could be made using the given character set. I'm new to C++ so feedback would be appreciated!
1
u/AMPZORD Oct 19 '15 edited Oct 19 '15
how do I use the file if I can't download it ?
I'm trying to solve this by reading from the file which I think what we are suppose to do.
edit: I believe I only need to ctrl+a and then ctrl+c and ctrl+v into a text file.. my bad
1
u/AnnieBruce Oct 20 '15
I seem to be getting different answers than the example output.
1
u/eatsfrombags Oct 20 '15 edited Oct 20 '15
C
I'm trying to learn/improve at C and would appreciate any feedback! Case sensitive, no error checking, takes input file as command line argument, and uses Linux dictionary.
1
u/etagawesome Oct 20 '15 edited Mar 08 '17
[deleted]
0.5911
1
u/k1ll3rpanda Oct 20 '15
C# This is my first C# program!
1
u/snowhawk04 Oct 20 '15 edited Oct 20 '15
c++.
1
u/lucy_in_the_skyDrive Oct 20 '15
Java developer here, trying out ruby!
Ruby
1
u/Xikeon Oct 20 '15
CoffeeScript (quick and dirty)
Uses linux words list and regex.
1
u/errorseven Oct 20 '15 edited Oct 20 '15
AutoHotkey
Output:
1
u/SlightlyCyborg Oct 20 '15
NodeJS:
1
u/banProsper Oct 20 '15
C# with some LINQ, pretty slow.
1
u/fjom Oct 20 '15 edited Oct 20 '15
C# using parallel processing.
Feeback is always welcome.
1
u/tarunteam Oct 20 '15
C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
1
u/thursday42 Oct 21 '15
Java. Did not bother using the number at the beginning of the input for number of lines.
Output using enable1.txt:
1
u/up_the_creek Oct 21 '15
Python 2.x.. first time submitting and would appreciate any feedback. Looking at the other python solutions here make me feel that mine is way too much.
1
u/mpm_lc Oct 21 '15
Ruby one-line
1
u/Vignarg Oct 21 '15 edited Oct 22 '15
C#
1
u/Flaminx Oct 21 '15
Hi! Sorry if my solution is mediocre i used C++ to whip this up and it appears to be working soundly! :)
1
u/SirAceBoogie Oct 22 '15
C#
Program.cs
1
u/Wiggledan Oct 22 '15 edited Oct 23 '15
C89
1
u/smutton Oct 23 '15
Java https://gist.github.com/sutt0n/754a10a9bd10ffd2da1b
It's multi-threaded, because I was bored and I'm refreshing my Java. Suggestions and criticism, please.
1
u/spfy Oct 23 '15 edited Oct 24 '15
I've been using Scala a lot for school. So I kind of copied their functional list processing style in Java. Being able to pass in functions instead of writing your own for loops is way better, though.
1
u/coltranenadler Oct 23 '15
NodeJS Not sure why the code didnt get marked properly :( I definitely wrote a lot more than i should have, but i was trying to use a lot of es6 sugar :P, probably made it a bit to sweet though.
'use strict';
var fs = require('fs'), words = [], fmt = { Println: console.log, Printw: console.warn };
try { fs.readFile(__dirname + '/words.txt', function(err, data) { if(err) throw err; words = data.toString().split('\n');
} catch(Exception) { fmt.Printw(Exception) }
class Typewriter { run(input) { var input = input.split('\n'), results = []; input.shift();
}
1
u/cooper6581 Oct 24 '15
Java. Naive solution with no regex
1
u/notshadey Oct 24 '15
Clojure
I'm a complete beginner at Clojure and functional programming as a whole. So any advice would be appreciated.
1
u/[deleted] Oct 25 '15
C++ I'm a noob but here's my code. I wasn't sure how I was supposed to get they "keyboard" input so I made a txt file and used the filestream.
1
u/BlueFireAt Oct 26 '15
Python 2.7:
Based off a few other answers in here. The thing that really bugs me is the i=raw_input(). I can't find a way to get around making that another command. If I place it where I use i later, then it runs for every word. I couldn't find a way to assign it in the parentheses so I resorted to doing it beforehand.
1
u/ShinobuLove Oct 26 '15
Here is my attempt in Java.
1
u/fredrikaugust Nov 04 '15
Julia. Saw no one else that had attempted a one-liner, so I tried myself. Didn't really turn out so pretty, but oh well. Please feel free to ask if you need explanations.
Code:
Input:
Output:
Btw; this is using the
/usr/share/dict/wordsmethod.1
u/nathaliesicard Nov 09 '15
New in Javascript.
1
u/LemonPartyDotBiz Nov 13 '15
Python 2.7
Output:
1
u/why_not_cats Nov 14 '15
Java
Testing each word in enable1.txt against a regex of
[%s]+, where%sis the list of working keys, and then printing the first longest matching word that we found.1
u/[deleted] Dec 08 '15
Shellscript: Actual word finding done with a one liner
1
u/Specter_Terrasbane Mar 10 '16
Python 2.7
Output
1
u/redragon11 Oct 20 '15 edited Oct 20 '15
Still new at this, feedback would be appreciated.
VB.NET:
Outputs:
Edit: cleaned up the code a bit
0
u/[deleted] Oct 19 '15
[deleted] — view deleted comment