Hello,
I was inspired by Tom Scott's 7-seg video : https://www.youtube.com/watch?v=zp4BMR88260
In the closing lines he poses a challenge, that if there are two longest words that you can display we'd skip over them
I think i've fixed it : in Node.js :
Code:
I think it's working.
But, I want to be able to debug output in an interactive console better then Node.js's REPL, just to make sure
So I went to Ruby, this is where problems arose
Heres my equivalent ruby code :
Code:
It should output "supertranscendentness" like the Node.js program does.
But, it doesn't output anything, did I translate anything wrong?
EDIT : I cleaned up the beginning part (that sorts words.txt into an array) to declare what it's doing and made it run a bit faster (as fast as I can make ruby go)
Code:
That seemed to fix it
One question, is this an optimal way of solving this problem? I know the code is messy and was originally written in Javascript, Mateo. Apart from those two things, is there a better way to do this?
I was inspired by Tom Scott's 7-seg video : https://www.youtube.com/watch?v=zp4BMR88260
In the closing lines he poses a challenge, that if there are two longest words that you can display we'd skip over them
I think i've fixed it : in Node.js :
Code:
var fs = require("fs");
var words = fs.readFileSync("words.txt").toString();
words = words.split("\n");
var badLetters = /[gkmqvwxzio]/;
var currentLongestWord = "";
var longestWords = [];
for (var testWord of words) {
if(testWord.length <= currentLongestWord.length) {
continue;
} else if(testWord.length == currentLongestWord.length) {}
if(testWord.match(badLetters)) { continue; }
if(testWord.length == currentLongestWord.length) {
currentLongestWord = testWord;
longestWords.push(testWord);
}
if(testWord.length >= currentLongestWord.length) {
currentLongestWord = testWord;
}
}
if (longestWords === undefined || longestWords.length == 0) {
console.log(currentLongestWord);
} else {
console.log(longestWords);
}
I think it's working.
But, I want to be able to debug output in an interactive console better then Node.js's REPL, just to make sure
So I went to Ruby, this is where problems arose
Heres my equivalent ruby code :
Code:
words = [ ]
File.open("words.txt", "r") do |f|
f.each_line do |line|
words += line.split
end
end
badLetters = /[gkmqvwxzio]/
currentLongestWord = ""
longestWords = []
for testWord in words do
if testWord.length <= currentLongestWord.length then
next
elsif testWord.length == currentLongestWord.length then
end
if testWord.match(badLetters) then
next
end
if testWord.length == currentLongestWord.length then
currentLongestWord = testWord
longestWords.push(testWord)
end
if testWord.length >= currentLongestWord.length then
currentLongestWord = testWord
end
end
if longestWords == nil || longestWords.length == 0 then
puts "#{currentLongestWord}"
else
puts "#{longestWords}"
end
It should output "supertranscendentness" like the Node.js program does.
But, it doesn't output anything, did I translate anything wrong?
EDIT : I cleaned up the beginning part (that sorts words.txt into an array) to declare what it's doing and made it run a bit faster (as fast as I can make ruby go)
Code:
puts "sorting words ..."
words = File.read("words.txt")
words = words.split("\n")
badLetters = /[gkmqvwxzio]/
currentLongestWord = ""
longestWords = []
puts "finding solution ..."
=begin
*rest of code*
=end
That seemed to fix it
One question, is this an optimal way of solving this problem? I know the code is messy and was originally written in Javascript, Mateo. Apart from those two things, is there a better way to do this?