Last active
October 23, 2017 17:57
-
-
Save salmanx/97c1f1264054ca1ededad473dd0f55c5 to your computer and use it in GitHub Desktop.
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
# Sami’s spaceship crashed on Mars! She sends sequential SOS messages to Earth for help. | |
# Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal | |
# received by Earth as a string, S , determine how many letters of Sami’s SOS have been changed by radiation. | |
# Input Format | |
# There is one line of input: a single string, S . | |
# Note: As the original message is just SOS repeated n times, S ’s length will be a multiple of 3 . | |
# Constraints | |
# 1 ≤ |S| ≤ 99 | |
# |S| % 3 = 0 | |
# S will contain only uppercase English letters | |
# Output Format | |
# Print the number of letters in Sami’s message that were altered by cosmic radiation. | |
# Sample Input 0 | |
# SOSSPSSQSSOR | |
# Sample Output 0 | |
# 3 | |
# Sample Input 1 | |
# SOSSOT | |
# Sample Output 1 | |
# 1 | |
puts "give a sos messgae : " | |
sos_msg = gets.strip.upcase | |
# group the message with 3 letters and it will return an array | |
altered_msg = sos_msg.scan(/.{3}/) | |
changed_letter = 0 | |
altered_msg.each do |msg| | |
if msg != "SOS" | |
changed_letter = changed_letter + 1 | |
end | |
end | |
puts "Number of changed letter is #{changed_letter}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment