Once I created a formula with Perl and Elixir to solve this week’s Weekly Challenge, it was easy to run with it in Javascript and Python. It’s mostly a matter of changing the syntax. Yet, I still mixed things up a bit as I went along.
JavaScript Same String
Once again, I created a main() function like some kind of crazy C programmer just to have a function that can run each new set of arrays through the paces.
Most of this should look familiar:
// Same String
main(["ab", "c"], ["a", "bc"]);
main(["ab", "c"], ["ac", "b"]);
main(["ab", "cd", "e"], ["abcde"]);
function main( arr1, arr2 ) {
// 0
console.log('Input 1:')
console.log('@arr1 = (' + arr1 + ')')
console.log('@arr2 = (' + arr2 + ')')
// 1
const arr1_string = combine_list(arr1)
const arr2_string = combine_list(arr2)
const result = compare_strings(arr1_string, arr2_string)
console.log('Output: ' + result)
console.log('')
}
// 2
function combine_list( arr ) {
return arr.reduce((acc, str) => acc + str)
}
// 3
function compare_strings( str1, str2 ) {
return ( str1 == str2 ) ? true : false
}
0. I’m following the exercise writeup in prepending the array name with an encircled “A” to indicate it’s an array. Perl type checking, baby! I regret nothing.
1. I went back and forth on whether I should store the results of this function into a new variable and then use that as the arguments in the function on the next line. You could just as easily put this short function call inside the parenthesis in the compare_strings() call and be done with it.
I just thought this way of reading the code was a little easier, so I did it this way.
2. The reducer function is inside a function all its own. I thought it looked ugly to inline that function twice in main(), so it went to the side on its own. I love a good reducer. You just pass the item you’re iterating over with the accumulator, itself, and a function to do the dirty work. Voila!
3. Yes! A ternary operator! Come at me. I’m fine with it. A lot of people hate ternaries, but there’s nothing wrong with them when used right. This is a prime example of “right.” It only gets ugly when you have a complicated “if” statement or you start nesting ternaries, but for a simple true/false test like this, it makes perfect sense. I didn’t need one in Elixir because I was using pattern matching. I used it as an option in Perl, but I should have committed to it. Maybe I’ll go back and fix the script on Github…
“Same String” Simplified: Python Edition
I’m forcing myself to learn Python, because I need it for work. With a few tweaks, it’s just Perl/Ruby/Javascript with white space that counts. I keep bouncing between languages and forgetting which ones use [] for arrays/lists versus (). Also, this is the fourth version of this code I’ve written, so maybe I’m just getting tired?
The good news is, Python does have a built-in reducer function, but you do need to import it from a core library. I know Perl has things like this, too, but it’s always happening for me in Python. I’m always having to import something from a core library to use in my scripts, and that feels clunky.
I get it — why waste memory on lots of libraries if you don’t need them, but can’t the language automatically include them when it seems they are being used? It’s still better than having to install them, but it does annoy me.
Let’s get right to the code:
from functools import reduce
def show_inputs(arr1, arr2):
print( "Input: @arr1 = ", arr1 )
print( " @arr2 = ", arr2 )
def reduce_string(arr):
return reduce(lambda x, y: x + y, arr)
## 1
def compare_strings(str1, str2):
return "true" if str1 == str2 else "false"
def show_output(final_answer):
print( "Output: ", final_answer, "\n" )
## 2
def run_everything(arr1, arr2):
show_inputs(arr1, arr2)
str1 = reduce_string(arr1) ## 3
str2 = reduce_string(arr2)
show_output( compare_strings(str1, str2) )
## Run process with example data
run_everything(["ab", "c"], ["a", "bc"] )
run_everything(["ab", "c"], ["ac", "b"] )
run_everything(["ab", "cd", "e"], ["abcde"] )
I like how compact this code feels. It’s very thin. It gets straight to the point and doesn’t waste a lot of space. The reduce function with the lambda will take some getting used to, but it does the job.
- You can display a boolean in Python in a print statement by using the str() function. But why bother when, for the sake of this exercise, I just need a string to print out? That’s why I’m returning the string here instead of the Boolean. Note that booleans in Python start with a capital letter, but my strings here do not.
- Is there a Pep yet for a pipe operator? That might clean some of this up.
- Since there is no pipeline operator, there’s really no reason to call a single function with two arrays to get back a tuple or an array of two arrays back, right? So I call directly into the reducer here, instead of the middle step of having a function do that twice.
It is also at about this point that I realized my quest to finish the first challenge in four different languages means I never did the second challenge of the week.
Better luck next week, me!