Skip to content

Instantly share code, notes, and snippets.

@roman-on
Created May 17, 2020 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roman-on/8d271ff5e105ee3658d860614c683973 to your computer and use it in GitHub Desktop.
Save roman-on/8d271ff5e105ee3658d860614c683973 to your computer and use it in GitHub Desktop.
"""
The function accepts as a path parameter for a text file (string).
The path whose path is passed as an argument contains a list of integers from 1 to n that are not sorted, comma separated, with one of the numbers in the sequence disappearing (i.e. missing from the sorted list).
Who_is_missing function operation:
A. The function returns the missing number in sequence (int).
B. The function writes the missing number in succession to a new file called found.txt.
Example of an input file and running the who_is_missing function
A file called findMe.txt:
8,1,9,7,4,6,3,2
Run the who_is_missing function on the findMe.txt file:
>>> who_is_missing ("c: \ findMe.txt")
5
After the above run of the who_is_missing function, a new file called found.txt is created:
5
"""
"""
A. The function returns the missing number in sequence (int).
B. The function writes the missing number in succession to a new file called found.txt.
"""
def who_is_missing(file_name):
destination = r"R:\D\Studies\! Python\! Israeli Python\Small programs\Part of program files\4\found.txt" # Your new destination
a = "" # New variable
c = "" # New variable
with open (file_name, "r") as file_name: # Opening the file and closing automatically after finishing to use
for num in file_name: # Printing the lines from the file
a += num # Adding the lines in to the new variable
b = int(max(a)) # Get the maximum number from the listed numbers in the file in order to use it for the loop
for i in range(1, b): # The loop starts from one and going up to maximum number from the file
if str(i) not in a: # Converting variable "i" from the loop into str in order to check if there is a number like that in the file and if not so you add this number to the new variable "c"
c += str(i) # If there is a no such number in the file it will add it to variable "c"
with open (destination, "w") as destination: # Opens a file destination to edit it by "w" and copy the result to it
destination.write(c) # Writting the result to the new file destination
def main():
who_is_missing(r"C:\.....")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment