ulvis.paste.net

Paste Search Dynamic
Recent pastes
table
  1. def createTable( expensesTable ):           #DONE
  2.     """
  3.    Creates the expenses table for the month for the days [1, 30]
  4.    Input = an empty dictionary, that gets modified
  5.    """
  6.  
  7.     for i in range( 1 , 31 ):
  8.         expensesTable[i] = { "housekeeping" : [] , "food" : [] , "transport" : [] , "clothing" : [] , "internet" : [] , "others" : [] }
  9.  
  10. def AddEntries():
  11.     Insert( [ "1" , "10" , "food" ] )
  12.     Insert( [ "1" , "29" , "housekeeping" ] )
  13.     Insert( [ "1" , "12" , "transport" ] )
  14.  
  15.  
  16. def printCommands():                        #DONE
  17.     '''
  18.    Prints the existing commands
  19.    '''
  20.     print("add <sum> <category>")
  21.     print("insert <day> <sum> <category>")
  22.     print(" ")
  23.     print("remove <day>")
  24.     print("remove <start day> to <end day>")
  25.     print("remove <category>")
  26.     print(" ")
  27.     print("list")
  28.     print("list <category>")
  29.     print("list <category> [ < | = | > ] <value>")
  30.  
  31. def parseCommand(userInput):                #DONE
  32.     '''
  33.    Receives the user's input as a string, and returns the command(lowercased) and parameters(lowercased)
  34.     '''
  35.    if userInput.find( " " ) != -1:
  36.        command = userInput[0: userInput.find(" ")].lower()
  37.        params = userInput[userInput.find(" "):].split()
  38.        for i in range( 0, len( params ) ):
  39.            params[i] = params[i].lower()
  40.  
  41.    else:
  42.        command = userInput.lower()
  43.        params = []
  44.    return (command, params)
  45.  
  46. def isCommandValid( command ):            #DONE
  47.    '''
  48.     Returns true if the command exists in the list of commands
  49.             false- otherwise
  50.     '''
  51.    l = ["add", "insert", "remove", "list", "quit"]                    
  52.    if command in l:                                    
  53.        return True
  54.    else:
  55.        return False
  56.  
  57. def areParamsValid( command , params ):    
  58.    """
  59.    Input- a command(string), and its parameters(list of strings)
  60.    Output:
  61.            True if the parameters are used correctly for the specific command
  62.            False otherwise
  63.    """
  64.  
  65.    paramsList = [ "housekeeping" , "food" , "transport" , "clothing" , "internet" , "others" ]
  66.    if command == "add":
  67.    #add <sum> <category>
  68.        if len( params ) == 2:
  69.            #check if first parameter is an int
  70.            try:
  71.                int( params[0] )
  72.            except ValueError:
  73.                return False    
  74.            #check if second parameter is one of the categories
  75.            if params[1] in paramsList:
  76.                return True
  77.            else:
  78.                return False
  79.        else:
  80.            return False
  81.    elif command == "insert":
  82.    #insert <day> <sum> <category>
  83.        if len( params ) == 3:
  84.            #check if first parameter is an int between 1 and 30
  85.            try:
  86.                int( params[0] )
  87.            except ValueError:
  88.                return False
  89.            if int( params[0] ) < 1 or int( params[0] ) > 30:
  90.                return False
  91.            #check if second parameter is an int
  92.            try:
  93.                int( params[1] )
  94.            except ValueError:
  95.                return False
  96.            #check if third parameter is one of the categories
  97.            if params[2] in paramsList:
  98.                return True
  99.            else:
  100.                return False
  101.        else:
  102.            return False
  103.    elif command == "remove":
  104.        if len( params ) == 1:
  105.            #check if the parammeter is a category
  106.            if params[0] in paramsList:
  107.                return True
  108.            #check if the parameter is an int between 1 and 30
  109.            try:
  110.                int( params[0] )
  111.            except ValueError:
  112.                return False
  113.            if int( params[0] ) < 1 or int( params[0] ) > 30:
  114.                return False
  115.            return True
  116.        elif len( params ) == 3:
  117.            #check if first parameter is an int between 1 and 30
  118.            try:
  119.                int( params[0] )
  120.            except ValueError:
  121.                return False
  122.            if int( params[0] ) < 1 or int( params[0] ) > 30:
  123.                return False
  124.            #check if second parameter is "to"
  125.            if params[1] != "to":
  126.                return False
  127.            #check if third parameter is an int between 1 and 30
  128.            try:
  129.                int( params[2] )
  130.            except ValueError:
  131.                return False
  132.            if int( params[2] ) < 1 or int( params[2] ) > 30:
  133.                return False
  134.            #check if first day is smaller than second day
  135.            if int( params[0] ) > int( params[2] ):
  136.                return False
  137.            return True
  138.        else:
  139.            return False
  140.    elif command == "list":
  141.        if len( params ) == 0:
  142.            return True
  143.        elif len( params ) == 1:
  144.            #check if parameter is a category
  145.            if params[0] in paramsList:
  146.                return True
  147.            else:
  148.                return False
  149.        elif len( params ) == 3:
  150.            #check if first parameter is a category
  151.            if not params[0] in paramsList:
  152.                return False
  153.            #check if second parameter is a sign
  154.            if params[1] != "<" and params[1] != "=" and params[1] != ">":
  155.                return False
  156.            #check if third parameter is an int
  157.            try:
  158.                int( params[2] )
  159.            except ValueError:
  160.                return False
  161.            return True
  162.        else:
  163.            return False
  164.    elif command == "quit":
  165.        if len( params ) == 0:
  166.            return True
  167.        else:
  168.            return False
  169.    else:
  170.        print("this shouldn't happen")
  171.        return False
  172.                      
  173. def checkCommand( command , params ):
  174.    """
  175.     Input: a command(string) , and its parameters(list of strings)
  176.     Output:
  177.             true if the command is valid and its parameters are correctly used
  178.             false otherwise
  179.     """
  180.  
  181.    is_command_valid = isCommandValid( command )
  182.    if is_command_valid:
  183.        return areParamsValid( command , params )
  184.    else:
  185.        return False
  186.  
  187. def test_checkCommand():
  188.    """
  189.     Tests the function checkCommand()
  190.     No input or output
  191.     """
  192.  
  193.    #testing add
  194.    assert checkCommand( "add" , [ "50" , "clothing" ] ) == True                            #Correct form
  195.    assert checkCommand( "add" , [ "50" , "abcd" ] ) == False                               #Wrong category
  196.    assert checkCommand( "add" , [ "abc" , "clothing" ] ) == False                          #Parameter of wrong type
  197.    assert checkCommand( "add" , [ "50" , "clothing" , "dead weight" ] )  == False          #More parameters than needed
  198.    assert checkCommand( "add" , [ "50" ] ) == False                                        #Less parameters than needed
  199.  
  200.    #testing insert
  201.    assert checkCommand( "insert" , [ "20" , "50" , "housekeeping" ] ) == True              #Correct form
  202.    assert checkCommand( "insert" , [ "20" , "50" , "abc" ] ) == False                      #Wrong category
  203.    assert checkCommand( "insert" , [ "oh" , "50" , "clothing" ] ) == False                 #Parameter of wrong type
  204.    assert checkCommand( "insert" , [ "20" , "oh" , "clothing" ] ) == False                 #Parameter of wrong type
  205.    assert checkCommand( "insert" , [ "20" , "50" , "housekeeping" , "2" ] ) == False       #More parameters than needed
  206.    assert checkCommand( "insert" , [ "20" , "50" ] ) == False                              #Less parameters than needed
  207.    assert checkCommand( "insert" , [ "40" , "50" , "food" ] ) == False                     #Day bigger than 30
  208.    assert checkCommand( "insert" , [ "-1" , "50" , "food" ] ) == False                     #Day smaller than 1
  209.  
  210.    #testing remove
  211.    assert checkCommand( "remove" , [ "20" ] ) == True                                      #Correct form
  212.    assert checkCommand( "remove" , [ "food" ] ) == True                                    #Correct form
  213.    assert checkCommand( "remove" , [ "31" ] ) == False                                     #Day bigger than 30
  214.    assert checkCommand( "remove" , [ "0" ] ) == False                                      #Day smaller than 1
  215.    assert checkCommand( "remove" , [ "f" ] ) == False                                      #Wrong category
  216.    assert checkCommand( "remove" , [ "1" , "2" , "3" , "4" , "5" ] ) == False              #More parameters than needed
  217.    assert checkCommand( "remove" , [] ) == False                                           #Less parameters than needed
  218.    assert checkCommand( "remove" , [ "1" , "to" , "30" ] ) == True                         #Correct form
  219.    assert checkCommand( "remove" , [ "20" , "to" , "19" ] ) == False                       #Day one smaller than day 2
  220.    assert checkCommand( "remove" , [ "30" , "to" , "31" ] ) == False                       #Day bigger than 30
  221.    assert checkCommand( "remove" , [ "0" , "to" , "30" ] ) == False                        #Day smaller than 1
  222.    assert checkCommand( "remove" , [ "1" , "AA" , "20" ] ) == False                        #Second parameter is wrong
  223.  
  224.    #testing list
  225.    assert checkCommand( "list" , [ ] ) == True                                             #Correct form
  226.    assert checkCommand( "list" , [ "clothing" ] ) == True                                  #Correct form
  227.    assert checkCommand( "list" , [ "clothing" , "<" , "50" ] ) == True                     #Correct form
  228.    assert checkCommand( "list" , [ "food" , "=" , "40" ] ) == True                         #Correct form
  229.    assert checkCommand( "list" , [ "food" , ">" , "50" ] ) == True                         #Correct form
  230.    assert checkCommand( "list" , [ "food" , "aa" , "40" ] ) == False                       #Sign is wrong
  231.    assert checkCommand( "list" , [ "clothing" , "<" , "pf" ] ) == False                    #Wrong type
  232.    assert checkCommand( "list" , [ "clothing" , "=" ] ) == False                           #Wronng number of parameters
  233.    assert checkCommand( "list" , [ "1" , "2" , "3" , "4" , "5" ] ) == False                #Too many parameters
  234.  
  235.    #testing quit
  236.    assert checkCommand( "quit" , [] ) == True                                              #Correct form
  237.    assert checkCommand( "quit" , [ "1" ] ) == False                                        #Too many parameters
  238.    
  239.    #Wrong command
  240.    assert checkCommand( "aaa" , [] ) == False                                              #Command doesn't exist
  241.    #print("Everything is fine")
  242.  
  243. def Troubleshoot( command ):
  244.    """
  245.     Lets the user know if they inserted a wrong command or wrong parameters for that specific command
  246.     Input: a command(string)
  247.     """
  248.  
  249.    if not isCommandValid( command ):
  250.        print( command + " is not a valid command." )
  251.    else:
  252.        print( "Wrong use of parameters for the command " + command )
  253.  
  254. def Add( current_day , params , expensesTable ):
  255.    """
  256.     add <sum> <category> - Adds a sum to the specified category for the current day in the expenses table
  257.     Input: the current day(int) and the parameters - sum and category(list of strings), and the expenses table that has to be modified
  258.     Output: modifies expensesTable, doesn't return anything
  259.    """
  260.    expensesTable[current_day][params[1]].append( int(params[0] ) )
  261.  
  262. def Insert( params, expensesTable ):
  263.    """
  264.    insert <day> <sum> <category> - Inserts a sum to the specified category for the specified day
  265.    Input: the parameters(list of strings) , and the expenses table that has to be modified
  266.    Output: modifies expensesTable, doesn't return anything
  267.     """
  268.    expensesTable[ int( params[0] ) ][params[2]].append( int( params[1] ) )
  269.  
  270. def Remove( params , expensesTable ):
  271.    """
  272.     remove <day>                                        -Removes all expenses for the given day
  273.     remove <start day> to <end day>                     -Removes all expenses in the [start day, end day] interval
  274.     remove <category>                                   -Removes all the expenses for a given category
  275.     Input: the parameters(list of strings), and the expenses table that has to be modified
  276.     Output: modifies expensesTable, doesn't return anything
  277.    """
  278.    if len( params ) == 1:
  279.        #check if it is an int
  280.        is_int = True
  281.        try:
  282.            int( params[0] )
  283.        except ValueError:
  284.            is_int = False
  285.        
  286.        if is_int:
  287.            for key in expensesTable[ int( params[0] ) ]:
  288.                expensesTable[ int( params[0] ) ][key] = []
  289.        else:
  290.            for i in range( 1, 31 ):
  291.                expensesTable[ i ][ params[0] ] = []
  292.    else:
  293.        for i in range( int( params[0] ) , int( params[2] ) + 1 ):
  294.            for key in expensesTable[ i ]:
  295.                expensesTable[ i ][key] = []
  296.  
  297. def List( params , expensesTable ):
  298.    """
  299.    list                                        -Lists all the expenses for the month
  300.    list <category>                             -Lists all the expenses for a given category
  301.    list <category> [ < | = | > ] <value>       -Lists the expenses for a given category that passes a certain criteria
  302.    """
  303.    something_was_printed = False
  304.    if len( params ) == 0:
  305.        for i in range( 1 , 31 ):
  306.            Print_Day = False
  307.            for key in expensesTable[i]:
  308.                if len( expensesTable[i][key] ) != 0:
  309.                    something_was_printed = True
  310.                    Print_Day = True
  311.                    break
  312.            if Print_Day:
  313.                print("___Day " + str( i ) + "___")
  314.                for key in expensesTable[i]:
  315.                    if len( expensesTable[i][key] ) != 0:
  316.                        print(key + ":")
  317.                        print( expensesTable[i][key] )
  318.    elif len( params ) == 1:
  319.        for i in range ( 1 , 31 ):
  320.            if len( expensesTable[i][ params[0] ] ) != 0:
  321.                something_was_printed = True
  322.                print("___Day " + str( i ) + "___")
  323.                print ( expensesTable[i][ params[0] ] )
  324.    else:
  325.        for i in range( 1 , 31 ):
  326.            if len( expensesTable[i][ params[0] ] ) != 0:
  327.                for x in expensesTable[i][ params[0] ]:
  328.                    if params[1] == "<":
  329.                        if int( x ) < int( params[2] ):
  330.                            something_was_printed = True
  331.                            print( str(x) + " " , end = "" , flush = True )
  332.                    elif params[1] == "=":
  333.                        if int( x ) == int( params[2] ):
  334.                            something_was_printed = True
  335.                            print( str(x) + " " , end = "" , flush = True )
  336.                    else:
  337.                        if int( x ) > int( params[2] ):
  338.                            something_was_printed = True
  339.                            print( str(x) + " " , end = "" , flush = True )
  340.    if not something_was_printed:
  341.        print("Nothing to be printed")
  342.  
  343.  
  344.  
  345. def start():
  346.    #Declaring variables
  347.    expensesTable = {  }
  348.    current_day = 1
  349.    createTable( expensesTable )
  350.  
  351.    #print( expensesTable )
  352.    printCommands()
  353.    test_checkCommand()
  354.    
  355.    while True:
  356.        userInput = input()
  357.        res = parseCommand(userInput)           #separates the command from parameters, lowercases everything
  358.        command = res[0]                        #the command
  359.        params = res[1]                         #the list of parameters
  360.        if checkCommand( command , params ):
  361.            if command == "add":
  362.                Add( current_day , params , expensesTable )
  363.            elif command == "insert":
  364.                Insert( params , expensesTable )
  365.            elif command == "remove":
  366.                Remove( params , expensesTable )
  367.            elif command == "list":
  368.                List( params , expensesTable )
  369.            else:
  370.                return
  371.        else:
  372.            Troubleshoot( command )
  373.            
  374.  
  375. start()
Parsed in 0.135 seconds