Created
December 24, 2020 03:15
-
-
Save reymillenium/cb0b3e32081b457eb7fdcc826e870d91 to your computer and use it in GitHub Desktop.
Coding exercise for POSaBIT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write a small console application that uses a string comparison function that meets the following prototype: | |
# Entered or passed strings may be of any length; trap for edge conditions and display error messages. | |
# Function must return an int that is: | |
# <0 if string1 is less than string2; | |
# 0 if string1 is the same as string2; or | |
# >0 if string1 is greater than string2; | |
# and display the 2 strings as noted below. | |
# If the return value is <0, then returned strings are inverted, IE abcdef and uvwxyz, | |
# would be displayed as fedcba and zyxwvu | |
# If the return value is 0, then one string is returned and is made up with the merged | |
# 2 input strings; IE abcdef and uvwxyz, would be displayed as aubvcwdxeyfz. | |
# If the return value is >0, then one string is returned and is made up with the merged | |
# 2 input strings; IE abcdef and uvwxyz, would be displayed as zfyexdwcvbua | |
def comparing_strings(string1, string2) | |
returned_value = string1.size - string2.size | |
returned_string = case | |
when returned_value == 0 | |
"#{string1.chars.zip(string2.chars).flatten.join}" | |
when returned_value < 0 | |
"#{string1.reverse} and #{string2.reverse}" | |
else | |
Array.new([string1.size, string2.size].max) {|n| [ string2.reverse[n], string1.reverse[n] ] }.join | |
end | |
puts returned_string | |
returned_value | |
end | |
# If the return value is <0 | |
comparing_strings("abcdef", "tuvwxyz") | |
# If the return value is 0 | |
comparing_strings("abcdef", "uvwxyz") | |
# If the return value is >0 | |
comparing_strings("abcdef", "vwxyz") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Greetings,
This is a comment for the author of the exercise. There is few inaccuracies to consider in your exercise:
Note # 1:
That's not well redacted. It could be interpreted as the strings are the same (string1 == string2, as literally you are saying on the exercise, or maybe that string1.size == string2.size, (as I'm pretty sure that's what you really mean,... despite the fact that you are NOT saying it explicitly). Before that sentence, it was mentioned that the strings may be of any length, but that was on the previous paragraph, not in the sentence in question, so there is not necessarily a correlation between the previos paragraph and the sentence in discussion.
It should say instead:
Note # 2:
The phrase:
...is not properly separated from the rest. So during a while I was thinking: "he want's me to return an integer and now he says that he wants also several strings. So it's an array of different objects what he wants?"
You see, It's not that is not included that important piece of information on the exercise. It is included. The issue is that the position, unnecessarily attached to the definition of the ONLY returning int value,... is very distracting. Even grammatically speaking, that piece of information is not directly related to the definition of the only returning int value. It should be in a different paragraph. Otherwise it makes hard to understand the exercise.
Note # 3:
"abcdef and uvwxyz" are not proper and accurate examples of a returning value < 0 (string1's size is less than string2's size).
A proper example would be: "abcdef" and "tuvwxyz" (note the extra 't' at the beginning of the string2)
Note # 4:
You are exactly AND LITERALLY saying that the string to print on the terminal must be:
"fedcba and zyxwvu"
...and that's exactly how I solved the exercise. I'm sorry (because maybe that's not exactly what you mean), but that's EXACTLY what you asked me to do.
Note # 5:
Once again, "abcdef" and "uvwxyz" are not proper and accurate examples of a returning value > 0 (string1's size is greater than string2's size)
A proper example would be: "abcdef" and "vwxyz" (note the missing 'u' at the beginning of the string2)