You are on page 1of 5

Part 1

Question 1 : Evaluating programming best practices.

Solution B most closely follows the best programming practices

Explanation:

 Readability – Solution B has better code readability compared to Solution A and C. Presence of
very many nested if statements for solution A reduces readability making it difficult to track the
actual flow of logic intended by the if statements. Solution B simplifies the logic of finding
whether a ward is palindrome by representing it in a function and using for loop which makes it
more readable compared to many if statements that are used in solution C which reduces
readability.
 Modularity – Solution B is more modular compared to solution A and C. It has the main
functionality represented using functions including reverse_word, is_palindrome and
check_all_palindrome. This structure enables one to know what each piece of code intends to
achieve as a part of the final goal of solving the palindrome problem.

Question 2: Identifying functions’ time and space efficiency

Function A is the most efficient function

Explanation:

 Function B has the lowest memory efficiency since it creates the variable factors that stores the
generated array of numbers from 2 to n-1 inclusive which occupies more space compared to
Function A and C which does not store the array of numbers.
 Function C introduces many logical ‘and’ expressions which must all be evaluate hence
increasing the time required to execute the function.
 Function A on the other hand uses square root in the for loop despite the fact that performing
square root functions takes more time, it is better time efficiency compared to the logical ‘and’ in
function C since square root is performed on once while logical ‘and’ evaluation is done for each
loop iteration in function C from 1 to n inclusive

Question 3: Coding Task Problem Solving.

Initialize an empty dictionary frequency_map

While n is greater than 0

digit = n mod 10

if digit is not in frequency map keys

add digit to the frequency map with an initial value of 1

else add 1 to the value of the digit in the frequency_map

n = n // 10
PART 2: RESPONSE COMPARISONS

Question 4: Side by Side Response comparison – General Replies

Response A is better to provide compared to response B.

Explanation:

 Response A refuses to help on tasks that invades someone’s privacy, highlights the negative
repercussions of engaging in such activity and suggests to discuss with the user more positive
topics which in turn will direct the user aways from invading someone else privacy.
 Response B discourages the action deeming it highly unethical and possibly illegal but goes on to
give instructions to the user on the steps to achieve hence promoting the malicious intention.

Question 5 : Side by Side Response comparison – General Replies

Response B is more helpful.

Explanation:

 Response A declares and initializes the step and subsets variables inside the loop. This will result
to low memory efficiency as the values of the variables will be reinitialized in the memory at
every iteration hence lowering the memory efficiency of code in response A.
 Response B on the other hand declares and initializes variables outside the loop. This will make
it more memory efficient as the value of the step and subsets variables will be updated as the
loop progresses instead of reinitializing and updating the values of the variables as in the case of
response A.

PART 3: Coding Exercise: Decoding a message from a text file

Here is the code “def decode_and_sort_from_file(filename):

try:

with open(filename, 'r') as file:

# Read the content from the file

input_str = file.read()

# Split the input string into lines

lines = input_str.strip().split('\n')

# Create a list of tuples containing the numeric value and the content

data = [(int(line.split(':')[0]), line.split(':')[1].strip()) for line in lines]


# Sort the list of tuples based on the numeric values

sorted_data = sorted(data, key=lambda x: x[0])

# Extract the sorted content

sorted_content = [content for num, content in sorted_data]

# Create a list to store the numeric values

numeric_values = []

# Loop over the sorted content, extract and append the numeric values to the list

for line in sorted_content:

num_str = line.split(':')[0].strip()

numeric_values.append(num_str)

return numeric_values

except FileNotFoundError:

print(f"Error: File '{filename}' not found.")

return None

def arithmetic_progression(start, end, constant_diff_increase):

"""Generates an arithmetic progression with increasing constant difference."""

progression = [start]

constant_diff = 1

for i in range(1, end - start):

constant_diff += constant_diff_increase

next_value = progression[-1] + constant_diff

if next_value < end:

progression.append(next_value)
else:

break

return progression

result_list = decode_and_sort_from_file("message.txt")

# Generate the index pattern using the arithmetic progression function

index_pattern = arithmetic_progression(0, min(10, len(result_list)), 1) # Up to 10 indices or max length

# Iterate and print values in a single line

values_to_print = [result_list[index] for index in index_pattern if index < len(result_list)]

print(' '.join(values_to_print))

This code has two functions:

1. decode_and_sort function: It takes a string as input containing lines with numeric values
followed by a colon and content. Each line is split into a numeric value and a content string. The
function then sorts the list of tuples in ascending order based on the numeric values and returns
a list of the sorted content strings.

2. arithmetic_progression function: It generates a list of numbers in an arithmetic progression,


where the difference between consecutive numbers increases by a constant value. The function
takes three arguments: the start number, the end number, and the constant difference increase.
It initializes a list with the starting value. It then adds values to the progression involving
increasing the constant difference. It stops when the proceeding value is greater than the
specific end value. It then returns back the progression that has been generated in the process
as a list.

3. The decode_and_sort function is then called passing the input_str as argument which can
should be replaced with the file name for example

Tell Us About Yourself:

Data Science

C, C++, Python, Java, Kotlin, Go, HTML/CSS, JavaScript, NoSQL, PHP and R, SQL, Docker, Cloud
Computing, Unit and Functional Tests, Machine learning and Data Visualization.
Man, Black or African American(Modify if necessary), Fluent English Speaker, Africa(Modify is
necessary), Bachelors Degree

You might also like