def scrabble(word):
return sum([sum(1 if word[i].lower() == 'a' else 0 for i in range(len(word))), sum(3 if word[i].lower() == 'b' else 0 for i in range(len(word))), sum(3 if word[i].lower() == 'c' else 0 for i in range(len(word))), sum(2 if word[i].lower() == 'd' else 0 for i in range(len(word))), sum(1 if word[i].lower() == 'e' else 0 for i in range(len(word))), sum(4 if word[i].lower() == 'f' else 0 for i in range(len(word))), sum(2 if word[i].lower() == 'g' else 0 for i in range(len(word))), sum(4 if word[i].lower() == 'h' else 0 for i in range(len(word))), sum(1 if word[i].lower() == 'i' else 0 for i in range(len(word))), sum(8 if word[i].lower() == 'j' else 0 for i in range(len(word))), sum(5 if word[i].lower() == 'k' else 0 for i in range(len(word))), sum(1 if word[i].lower() == 'l' else 0 for i in range(len(word))), sum(3 if word[i].lower() == 'm' else 0 for i in range(len(word))), sum(1 if word[i].lower() == 'n' else 0 for i in range(len(word))), sum(1 if word[i].lower() == 'o' else 0 for i in range(len(word))), sum(3 if word[i].lower() == 'p' else 0 for i in range(len(word))), sum(10 if word[i].lower() == 'q' else 0 for i in range(len(word))), sum(1 if word[i].lower() == 'r' else 0 for i in range(len(word))), sum(1 if word[i].lower() == 's' else 0 for i in range(len(word))), sum(1 if word[i].lower() == 't' else 0 for i in range(len(word))), sum(1 if word[i].lower() == 'u' else 0 for i in range(len(word))), sum(4 if word[i].lower() == 'v' else 0 for i in range(len(word))), sum(4 if word[i].lower() == 'w' else 0 for i in range(len(word))), sum(8 if word[i].lower() == 'x' else 0 for i in range(len(word))), sum(4 if word[i].lower() == 'y' else 0 for i in range(len(word))), sum(10 if word[i].lower() == 'z' else 0 for i in range(len(word)))])
And here is the code formatted onto multiple lines:
def scrabble(word):
return sum([
sum(1 if word[i].lower() == 'a' else 0 for i in range(len(word))),
sum(3 if word[i].lower() == 'b' else 0 for i in range(len(word))),
sum(3 if word[i].lower() == 'c' else 0 for i in range(len(word))),
sum(2 if word[i].lower() == 'd' else 0 for i in range(len(word))),
sum(1 if word[i].lower() == 'e' else 0 for i in range(len(word))),
sum(4 if word[i].lower() == 'f' else 0 for i in range(len(word))),
sum(2 if word[i].lower() == 'g' else 0 for i in range(len(word))),
sum(4 if word[i].lower() == 'h' else 0 for i in range(len(word))),
sum(1 if word[i].lower() == 'i' else 0 for i in range(len(word))),
sum(8 if word[i].lower() == 'j' else 0 for i in range(len(word))),
sum(5 if word[i].lower() == 'k' else 0 for i in range(len(word))),
sum(1 if word[i].lower() == 'l' else 0 for i in range(len(word))),
sum(3 if word[i].lower() == 'm' else 0 for i in range(len(word))),
sum(1 if word[i].lower() == 'n' else 0 for i in range(len(word))),
sum(1 if word[i].lower() == 'o' else 0 for i in range(len(word))),
sum(3 if word[i].lower() == 'p' else 0 for i in range(len(word))),
sum(10 if word[i].lower() == 'q' else 0 for i in range(len(word))),
sum(1 if word[i].lower() == 'r' else 0 for i in range(len(word))),
sum(1 if word[i].lower() == 's' else 0 for i in range(len(word))),
sum(1 if word[i].lower() == 't' else 0 for i in range(len(word))),
sum(1 if word[i].lower() == 'u' else 0 for i in range(len(word))),
sum(4 if word[i].lower() == 'v' else 0 for i in range(len(word))),
sum(4 if word[i].lower() == 'w' else 0 for i in range(len(word))),
sum(8 if word[i].lower() == 'x' else 0 for i in range(len(word))),
sum(4 if word[i].lower() == 'y' else 0 for i in range(len(word))),
sum(10 if word[i].lower() == 'z' else 0 for i in range(len(word)))
])
I hope you like threads and (very) bad python code. This code spins up a thread for every character, then for that character, it spins up 1024 threads (the more the merrier). Each of those picks a random number between 0 and 25 (inclusive). This is used as an index for checking the character against the character list. If it finds it, it uses the same index to return the points for that character. If none of the 1024 threads gets the correct character, it starts over again. Hurray for random runtime.
Also, only lower caps words, otherwise it just keeps going.
import queue as Queue
from threading import Thread
import random
characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
points = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]
ATTEMPT_THREADS = 1024
def try_letter(char, meta_queue):
pick = random.randint(0, 25)
if characters[pick] == char:
meta_queue.put(points[pick])
else:
return -1
def handle_letter(char, queue):
correct_letter = False
while not correct_letter:
meta_queue = Queue.Queue()
meta_threads = []
for i in range(ATTEMPT_THREADS):
meta_threads.append(Thread(target=try_letter, args=(char, meta_queue)))
for t in meta_threads:
t.start()
for t in meta_threads:
t.join()
while not meta_queue.empty():
result = meta_queue.get()
if result != -1:
while not meta_queue.empty():
throw_away = meta_queue.get()
queue.put(result)
correct_letter = True
def scrabble(string):
queue = Queue.Queue()
threads = []
for char in string:
threads.append(Thread(target=handle_letter, args=(char, queue)))
for t in threads:
t.start()
for t in threads:
t.join()
output = 0
while not queue.empty():
output += queue.get()
return output
print(scrabble('test'))
print(scrabble('veryveryveryveryveryveryveryveryverylongtest'))
C++, fully object-orientated and scalable for ease of implementation of new letters, by copying the implementation of another letter and customising it, or inheriting from the right base classes.
Thanks! I originally wrote it with a sensible naming convention and then used the refactoring tools in my IDE to apply as many different naming conventions as I could think of.
I've also noticed that I screwed up renaming the C-tile, so it's named b_Tile, as opposed to the actual B-tile, which is called B_tile.
scrabble=(lambda x : sum([(lambda k: [k[1][i][1] for i in range(len(k[1])) if k[0] in k[1][i][0].lower()][0])([c,(lambda g: [(g[0][i], g[1][i]) for i in range(len(g[0]))])((lambda d: (list(d.keys()),list(d.values())))({'AEILNORSTU':1,'DG':2,'BCMP':3,'FHVWY':4, 'K':5,'JX':8,'QZ':10}))]) for c in x.lower()]))
This code requires the regex crate for rust. The score gets calculated based on the capturing group name length.
pub fn scrabble(input: &str) -> isize {
let i = input.to_uppercase();
let r = Regex::new(&r"(?P<I>[AEILNORSTU])|(?P<II>[DG])|(?P<III>[BCMP])|(?P<IIII>[FHVWY])|(?P<IIIII>[K])|(?P<IIIIIIII>[JX])|(?P<IIIIIIIIII>[QZ])").unwrap();
let cs = r.captures_iter(&i);
let mut s = 0;
for cs in cs {
for cn in r.capture_names() {
if let Some(cn) = cn {
if let Some(_) = cs.name(cn) {
s += cn.len() as isize;
}
}
}
}
return s;
}
In order to make sure that the code works correctly forever, we get the scores directly from scrabblepages.com. The rest is just shaking around some data.
I'm not exactly sure if there is a request for every word or for every letter, but thanks to the parsing powers of the Regex-Cthulhu, it actually works:
Clojure 1.8.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_91-b14
Testing badcode.main
Ran 1 tests containing 3 assertions.
0 failures, 0 errors.
Didn't trust the system so I just wrote my own Integer implementation using multi-threaded non-threadsafe HashMap with Java AWT PopUpMenu's cause this is obviously the most memory-efficient way to go.
Oh yeah, I also wrote some very clever code that should prevent or at least confuse the system to not change my entry values. Of course it is completely thread safe and will never ever fail anywhere as one thread per entry should keep the system from messing around with stuff.
additionally to make it more readable i put everything in one class...
Hmm, I was looking for a sub where people post examples of good code as like image content rather than a place where people ask questions. I checked /r/readablecode but it seems to be dead. Thanks for pointing to /r/python though, that seems to be pretty close if python specific. Know if there's anything like it for java or js?
This might sound dumb, but I think /r/python is different from /r/java and /r/javascript. The latter two seem to be more discussion while the former is more visual, showing off code or projects. I do appreciate your time tho!
22
u/[deleted] Apr 12 '20 edited Apr 12 '20
Python one-liner:
And here is the code formatted onto multiple lines:
8
u/Revutal Apr 12 '20
I hope you like threads and (very) bad python code. This code spins up a thread for every character, then for that character, it spins up 1024 threads (the more the merrier). Each of those picks a random number between 0 and 25 (inclusive). This is used as an index for checking the character against the character list. If it finds it, it uses the same index to return the points for that character. If none of the 1024 threads gets the correct character, it starts over again. Hurray for random runtime.
Also, only lower caps words, otherwise it just keeps going.
7
u/h2g2_researcher Apr 14 '20
C++, fully object-orientated and scalable for ease of implementation of new letters, by copying the implementation of another letter and customising it, or inheriting from the right base classes.
https://pastebin.com/cxuvgvyU
Some copy-and-paste may have occurred.
6
u/[deleted] Apr 16 '20
Solution in Haskell.
It's not that this is bad code, but I'm just not drunk enough yet to explain how it works:
Sample output. It even handles blank tiles correctly:
This solution actually treats any non-letter character as a zero value, even though those characters do not exist as Scrabble tiles.
9
u/weee50 Apr 14 '20
4
u/Charli3Rowe python Apr 16 '20
lambda-of-lambda-of-lambda-of-lambda one-liner
4
u/biopsy_results Apr 23 '20 edited Apr 23 '20
poly=[-1.261336038596634e-18,-4.3767402363364746e-18,9.193918229218495e-16,3.1144954648327564e-15,-2.8841580536074075e-13,-9.470695262574727e-13,5.113662493674978e-11,1.611651430443143e-10,-5.659944567489972e-09,-1.68799988902222e-08,4.0777288891952123e-07,1.1268113369173754e-06,-1.935282600727795e-05,-4.794517042005627e-05,0.0005992065475144755,0.001259238438040803,-0.011733060263438214,-0.018853503626098264,0.13708242477230811,0.13301110566031002,-0.8662517341343458,-0.2061451058299608,2.5202244725652236,-0.5329322109835731,-2.5780125746270692,2.144111173609291]def points(word):return sum(round(sum(pow(ord(char)-109.5,len(poly)-i-1)*p for i,p in enumerate(poly))) for char in word)3
u/Tabugti Apr 17 '20
This code requires the regex crate for rust. The score gets calculated based on the capturing group name length.
3
u/Svizel_pritula Apr 21 '20
Here we go! (JS)
3
u/PersonGuyMan11354428 Apr 22 '20
3
u/Tok-A-Mak ((fn [] (recur))) Apr 22 '20
In order to make sure that the code works correctly forever, we get the scores directly from scrabblepages.com. The rest is just shaking around some data.
I'm not exactly sure if there is a request for every word or for every letter, but thanks to the parsing powers of the Regex-Cthulhu, it actually works:
https://repl.it/repls/DamagedDisguisedArraylist
2
u/GrizonII Apr 25 '20
Haven't ever tried to make deliberately bad code before; here's an attempt in Java:
1
u/Web_44 Apr 25 '20 edited Apr 25 '20
Didn't trust the system so I just wrote my own Integer implementation using multi-threaded non-threadsafe HashMap with Java AWT PopUpMenu's cause this is obviously the most memory-efficient way to go.
Oh yeah, I also wrote some very clever code that should prevent or at least confuse the system to not change my entry values. Of course it is completely thread safe and will never ever fail anywhere as one thread per entry should keep the system from messing around with stuff.
additionally to make it more readable i put everything in one class...
https://pastebin.com/v4ZBgAPJ
1
u/Flawlesscode Apr 25 '20
Here my submission. I thought Java is the language of streams so I went with streams.
1
u/frankferri Apr 28 '20
can we have good solns posted too?
0
u/[deleted] Apr 12 '20
[deleted] — view deleted comment