Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads in a filename provided by the user, opens it and read

ID: 3919394 • Letter: W

Question

Write a program that reads in a filename provided by the user, opens it and reads all of the lines. Create a dictionary to hold the line numbers for each valid identifier found. Your solution will need to process each line to determine whether a valid identifier exists on that line and if so, record both the line number and identifier found in the dictionary.

A valid identifier must be a string consisting of only letters, numbers, and underscores. If a line contains an invalid identifier no record is made in the dictionary. If a line contains a valid identifier already recorded in the dictionary append the line number to the dictionary log of where the identifier was found.

Here are some other requirements:

1.    Keep provided test file intact

2.    Use t4.py as your input test file to generate submission run output

3.    Verify identifiers are valid; ignore if invalid

----------------------------------------------------------------------------------------------------------------------------------------------

special characters such as "@", "!", and "." are considered invalid identifiers, underscores and numbers (can also start with such) are considered valid identifiers.

I have seen other examples but the use 'import re' I don't, however, know much about that library so if it could be done without using it, would be great!

the file I am using contains the following:

apple

1

1

ball

art

dog

&

pen

rat

4

apple

1@.

carrot

!

orange

ant

ant

ant

ball

stick

pen

_

$a

***

goodbye

Explanation / Answer

here is your program : --------------->>>>>>>>>>>>

def isNumber(arg):
    l = ["1","2","3","4","5","6","7","8","9","0"]
    for i in range(len(l)):
        if arg == l[i]:
            return True
    return False

def isLetterUnderscore(arg):
    s1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
    l = list(s1)
    for i in range(len(l)):
        if arg == l[i]:
            return True
    return False

def isValid(arg):
    for i in range(len(arg)):
        if isLetterUnderscore(arg[i]) or isNumber(arg[i]):
            i = i
        else:
            return False
    return True

def main():
    ident = {}#identifier dictionary
    try:
        mf1 = open("t4.py","r")
    except:
        print("Could not open the File ")
        return
    i = 1
    for line in mf1:
        line = line[:-1]
        if isValid(line):
            if line in ident:
                l = ident.get(line)
                l.append(i)
                ident[line] = l
            else:
                l = []
                l.append(i)
                ident[line] = l
        i = i+1
    print(ident)
       
main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote