Python Practice 1 2
Python Practice 1 2
Q 2. You are given an array (which will have a length of at least 3 but could be very
large) containing integers. The array is either entirely comprised of odd integers or
entirely comprised of even integers except for a single integer N. Write a method
that takes the array as an argument and returns this "outlier" N.
Examples
[2, 4, 0, 100, 4, 11, 2602, 36] --> 11 (the only odd number)
[160, 3, 1719, 19, 11, 13, -21] --> 160 (the only even number)
Only look at the hints below if you can’t think of an algorithm yourself.
# 1. You are given a list of numbers.
# 2. Most numbers in the list are either even or odd.
# 3. There is only one number that is different from the rest (the "outlier").
# 4. Look at the first three numbers in the list.
# 5. Check if at least two of them are even or at least two are odd.
# 6. This will tell you whether the majority of the list is even or odd.
# 7. Once you know the majority, go through the list and find the number that does
not match.
# 8. Return that number as the outlier.
Another way is:
# Hints to solve the problem:
# 1. You are given a list of numbers.
# 2. Most numbers in the list are either even or odd.
# 3. There is only one number that is different from the rest (the "outlier").
# 4. Create two empty lists: one for even numbers and one for odd numbers.
# 5. Loop through the list and check if each number is even or odd.
# 6. If the number is even, add it to the even list; if it is odd, add it to the odd list.
# 7. After looping through the list, check which list has only one number.
# 8. The list with one number contains the outlier, so return that number.
Q 3.
The Morse code encodes every character as a sequence of "dots" and "dashes". For
example, the letter A is coded as ·−, letter Q is coded as −−·−, and digit 1 is coded
as ·−−−−. The Morse code is case-insensitive, traditionally capital letters are used.
When the message is written in Morse code, a single space is used to separate the
character codes and 3 spaces are used to separate words. For example, the
message HEY JUDE in Morse code is ···· · −·−− ·−−− ··− −·· ·.
NOTE: Extra spaces before or after the code have no meaning and should be
ignored.
Your task is to implement a function that would take the morse code as input and
return a decoded human-readable string.
For example:
decode_morse('.... . -.-- .--- ..- -.. .')
#should return "HEY JUDE"
NOTE: For coding purposes you have to use ASCII characters . and -, not Unicode
characters.
All the test strings would contain valid Morse code, so you may skip checking for
errors and exceptions.
Testing:
Morse code to check:
.-- --- .-- .. - .-. .- -. ... .-.. .- - . -.. - .... .. ... ..-. .-. --- -- -- --- .-. ... .
Text to check:
Cat
You should be able to see this code: -.-. .- -