For a college assignment, I was tasked with the following (excerpt from provided code file):
Find one string of characters that is not present in the list. You are provided a function that loads the strings from the provided file,
sequences.txt
. You may not use any libraries that assist you in completing this solution. This specifically includes therandom
andstring
library. Your function must return two values: the string not present in the list, and a boolean value representing if the value is present in the list. Ensure to use documentation strings and comments to explain your program! Good luck!
Below is my solution, with a description of the algorithm is provided in the docstring. The main thing I'm worried about is my solution. I'm concerned there is a much easier solution, just not as elegant; like switching the first character of the first string and comparing that. It's not a 100% sure solution, but I'm sure it works most of the time. Mine is a sure solution, from my understanding.
I should mention, the file sequences.txt
contains 1000 lines, where each line is 1000 characters long, each character either A or B.
Any and all critiques and recommendations are welcome and considered!
"""
Find one string of characters that is not present in the list. You are provided a function
that loads the strings from the provided file, `sequences.txt`.
You may not use any libraries that assist you in completing this solution.
This specifically includes the `random` and `string` library.
Your function must return two values: the string not present in the list, and a boolean value
representing if the value is present in the list. Ensure to use documentation strings and comments
to explain your program! Good luck!
@professor XXXXXXXX
@class CS 1350
@due Nov 15, 2021
Provide your details below:
@author Ben Antonellis
@date Nov 2nd, 2021
"""
# PROVIDED FUNCTIONS #
def load_sequences():
sequences = []
with open('sequences.txt', 'r') as input_file:
for line in input_file:
sequences.append(line.strip())
return sequences
# PROVIDE YOUR SOLUTION BELOW #
from typing import Tuple # For code clarity
def find_excluded() -> Tuple[str, bool]:
"""
This algorithm marches through each string in the list, and for every string
flips the n-th character. By doing this, this ensures the string is different
from each string by at least one character.
"""
strings = load_sequences()
string_index = 0
result = ""
for idx, _ in enumerate(strings):
result += "A" if strings[idx][string_index] == "B" else "B"
string_index += 1
return result, result in strings
result, should_be_false = find_excluded()
print(result)
print(should_be_false)