0% found this document useful (0 votes)
4 views3 pages

Python Practice 1 2

The document outlines three coding challenges: defining a function to check if an integer is prime, identifying an outlier in an array of integers, and decoding Morse code into a human-readable string. Each challenge includes hints and examples to guide the implementation of the solutions. The focus is on algorithmic thinking and problem-solving techniques for each task.

Uploaded by

jtcgamming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python Practice 1 2

The document outlines three coding challenges: defining a function to check if an integer is prime, identifying an outlier in an array of integers, and decoding Morse code into a human-readable string. Each challenge includes hints and examples to guide the implementation of the solutions. The focus is on algorithmic thinking and problem-solving techniques for each task.

Uploaded by

jtcgamming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q 1.

Define a function that takes an integer argument and returns a


value true or false depending on if the integer is a prime.
# Hints to solve the problem. Only look at this if you can’t think of an
algorithm.
# 1. You are given a number and need to check if it is prime.
# 2. A prime number is a number greater than 1 that has only two factors: 1 and
itself.
# 3. If the number is 1 or less, it is not a prime number.
# 4. If the number is 2, it is a prime number because it has exactly two factors (1
and 2).
# 5. For any number greater than 2, check if it is divisible by any number from 2 to
the square root of the number. (Think remainder/mod %)
# 6. If the number is divisible by any of these, it is not prime.
# 7. If the number is not divisible by any of these, it is a prime number.

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: -.-. .- -

You might also like