top of page

Designing a Scrambler-Descrambler Program in Python

          For our 6th assignment in CSC 180, we were tasked with creating a scrambler-descrambler program. A copy of the assignment can be found here. I consider this design project a complete failure. When I began, I only skimmed over  the assignment before diving into the code. It took me a very long time to figure out what how to do what was needed, and I reached this solution only through trial and error. What's more, when I was double checking the assignment to make sure I had addressed everything, I realized that I had misunderstood the problem, and that my code wouldn't work if there was a blank line in the file (which was a requirement of the assignment). I had already completed most of the assignment and as a result did not want to restart, so I tried to design a band-aid solution. Trying to come up with this solution took as long as coming up with the rest of the program, and it took me 3 tries before I was able to get something that worked. 

 

Reflections

   

          This was a complete design failure, but I believe that we learn more through failure than we do through success, which is why I have included this here. It is my hope that others may learn from my mistakes and in doing so avoid them. In this project, I learned (the hard way) what happens when a designer doesn't use a design process. I was also reminded of the importance of actually reading the question. In a broader sense, this is analogous to identifying and understanding the problem before one begins to think of solutions. Another important lesson I learned is that a designer should never become so attached to their design that they refuse to iterate and generate new solutions. It would have probably been better if I rewrote my program. I knew the general idea from the first iteration, and I could have addressed the missing requirement better in the second iteration. 02/12/2013 - 2:28 AM

 

 

# Soren Sabet Sarvestany

# Created 11/02/2013, 9:23 PM

# This program removes line numbers from a file

# No me gusta el automarker! My code works!

 

def descramble(in_filename, out_filename):

 

    '''

    (file, file) --> file

    return the unscrambled version of a file

 

    '''

 

    # Open the files that will be used in program

    input_file = open(in_filename, 'r')

    input_file_counter = open(in_filename, 'r+')

    output_file = open(out_filename, 'w')

 

    # Create all of the counters we will need

    dig_in_line_num = 0

    line_num_counter = 1

    num_lines_in_input = 0

    didnt_find_line_num = 0

    num_elements_missing = 0

    last_line_check = 0

 

    # Count the number of lines in the file

    for line in input_file_counter:

        num_lines_in_input += 1

 

    #Add an extra line to the input file to avoid dealing with null characters

    input_file_counter.write("\n")

    input_file_counter.close()

 

    # Go through every line in the file

    for line in input_file:

        line_string = line

        last_line_check += 1

 

        # Go through the characters in each line

        for char in line_string:

            dig_in_line_num += 1

 

            # If the character is a colon, check if the line number we are looking

            # for is before the colon

            if char == ":":

                if line_num_counter == int(line_string[0:dig_in_line_num-1]):

                    line_string = '' + line_string[dig_in_line_num+1:]

                    output_file.write(line_string)

                    line_num_counter += 1

                    dig_in_line_num = 0

                    input_file.seek(0)

                    didnt_find_line_num = 0

 

                    # The above section strips the line number from the line and

                    # prints it to the file

                    break

                else:

                    dig_in_line_num = 0

                    didnt_find_line_num += 1

                    # if the line number wasn't found, go to the next line

                    break

 

        # These conditions deal with the scenario where a line is missing in the file                                                   The solution I eventually used to address the requirement I missed

 

        # This prevents the loop from infinitely continuing once the line counter

        # is larger than the largest line number value in the file

        if line_num_counter - num_elements_missing > num_lines_in_input:

            break

        

        # Prints a blank line if the line is missing from the file

        if didnt_find_line_num == num_lines_in_input:

            output_file.write("\n")

            num_elements_missing += 1

            input_file.seek(0)

            didnt_find_line_num = 0

            line_num_counter += 1

 

    # Close the files

    output_file.close()

    input_file.close()

 

            

            

     

        

if __name__ == "__main__":

    descramble("c:/Files/shorter.txt", "c:/Files/gonethroughdescrambler.txt")

 

# Other attempts at printing a blank line if the line is missing from the file                                                                  My first attempt at creating a solution for the requirement I missed

'''

        elif line_string == "" and ((line_num_counter + empty_line_count) < num_lines_in_input):

            empty_line_count += 1

            for empty_line_check in input_file:

                if empty_line_check == "":

                    empty_line_count += 1

                    continue

                else:

                    for char in empty_line_check:  # Removes the line number from the line

                        dig_in_line_num += 1

                

                        if char == ":":

                            if line_num_counter == int(line_string[0:dig_in_line_num-1]):

                                line_string = '' + line_string[dig_in_line_num+1:]

                                output_file.write(line_string + '')

                                line_num_counter += 1

                                dig_in_line_num = 0

                                input_file.seek(0)

                                break   

                            else:

                                dig_in_line_num = 0

                                didnt_find_line_num += 1

                                break

        elif (line_num_counter + empty_line_count + didnt_find_line_num) == num_lines_in_input:

             output_file.write("\n")

             line_num_counter += 1

             didnt_find_line_num = 0

             empty_line_count = 0

             input_file.seek(0)

             continue

'''

 

##                  if didnt_find_line_num < num_lines_in_input - missing_line_count:                                                      My second attempt at a solution for the missing requirement I missed              

##                        break

                    

##            if didnt_find_line_num == num_lines_in_input - missing_line_count:

##                line_num_counter += 1

##                missing_line_count += 1

##                didnt_find_line_num = 0

##                output_file.write("\n")

##                input_file.seek(0)

 

 

 

 

bottom of page