-1

I'm taking an online class and there are assignments. The on i'm working now has me comparing 3 numbers together to look for a match. Works fine with int's but when I pass a "two" into the function instead of a 2, i'm not able to compare and if I try to convert it, i'm not able to either.

Any ideas?

boolHomework = homework3Bonus(1,2,"two")

def homework3Bonus(a,b,c):
    print("The type of input is", type(c))
    strA = a
    strB = b
    strC = int(c)  #this isn't working
    print("The type of input is", type(a))
    print("The strings are",strA,strB,strC)
    if (strA == strB) or (strB == strC) or (strA == strC):
        print("Match")

This gives the error strC = int(c) ValueError: invalid literal for int() with base 10: 'two'

eezetee
  • 1
  • 3
  • 1
    Welcome to SO. ```int("two")``` doesn't work because ```two``` isn't a representation of a value integer. – ewokx May 17 '21 at 02:02
  • That's what I'm trying to figure out how to convert it... – eezetee May 17 '21 at 02:37
  • i thought strC = int(c) would take whatever is in C (say "two" in this example) and convert to an int? How could I compare a number and a string then? – eezetee May 17 '21 at 02:43
  • 1
    Yes, but "two" isn't the same as "2". It's a string that (for lack of a better phrase) represents the integer 2 in English. – ewokx May 17 '21 at 02:54
  • Yep. You clearly showed me the issue. That is why I tried to use the int(c) to convert it.. This was the challenge "bonus" in my online class. I've tried many ways to figure it out. None of them working. Basically the want me to compare 1,2,"two" and see if the are equal. – eezetee May 17 '21 at 02:57

2 Answers2

1

If you want a functional way to do it, which also looks for words up to 10, this solution will work.

It allows for int values 5 ints as a string '5' or word representations 'five' to be compared.

We apply map to all the incoming values to convert them to int where possible.

Then map a lambda over each combination of pairs in the list which compares and returns True or False for each combination. itertools is a python standard library.

any then checks if any value in our resulting list is True

import itertools

NUMBER_MAP = {
    'one': 1,
    'two': 2,
    'three': 3,
    'four': 4,
    'five': 5,
    'six': 6,
    'seven': 7,
    'eight': 8,
    'nine': 9,
    'ten': 10
}


def to_int(val):
    if isinstance(val, str):
        try:
            # convert number in string to int, e.g '2', '5', etc
            val = int(val)
        except ValueError:
            # convert work representation to int
            val = NUMBER_MAP.get(val.lower(), val)
    return val


def homework3Bonus(a, b, c):

    if any(map(lambda x: x[0] == x[1], itertools.combinations(map(to_int, (a, b, c)), 2))):
        print('Match')


homework3Bonus(3, 2, 'three')
homework3Bonus(3, 2, '3')
homework3Bonus(3, 2, 'two')

This could then easily be extended to take any number of arguments to find a match in an abitrary length of values.

def homework3Bonus(*args):

    if any(map(lambda x: x[0] == x[1], itertools.combinations(map(to_int, args), 2))):
        print('Match')


homework3Bonus(3, 2, 1, 5, '6', 'three')
Sam McHardy
  • 116
  • 1
  • 3
-1

Try this if you have low contraints for which it is possible to write a list stralpha manually as given below:

def homework3Bonus(a,b,c):
    a, b, c = str(a), str(b), str(c)
    lst = [a, b, c]
    
    max_possible = 10
    strnum = [str(i) for i in range(max_possible)]
    stralpha = ["zero", "one", "two", three", "four", "five", "six", "seven", "eight", "nine"]
    
    for i in range(3):
        if lst[i].isalpha():
            lst[i] = strnum[stralpha.index[lst[i].lower()]]

    return len(set(lst)) == 1
            

boolHomework = homework3Bonus(1, 2, "two")        # False
boolHomework = homework3Bonus("2", 2, "two")      # True

If you have higher contraints, you should refer to this: Is there a way to convert number words to Integers?

edusanketdk
  • 602
  • 1
  • 6
  • 11
  • OP wants to compare the string `one`, `two` etc with a literal number. – Kevin Yobeth May 17 '21 at 02:16
  • 1
    I really don't think that's what the homework is. He might have mis-conceptualized the question based on the trick case (1, "one", "1"), which should be False, as the question is **compare three integers**. – edusanketdk May 17 '21 at 02:19
  • Thank you both for your reply. Indeed I am suppose to compare a 2 with a "two" and see if they match.. Here is the assignment Extra Credit: Modify your function so that strings can be compared to integers if they are equivalent. For example, if the following values are passed to your function: 6,5,"5" You should modify it so that it returns true instead of false. Hint: there's a built in Python function called "int" that will help you convert strings to Integers. – eezetee May 17 '21 at 02:38
  • what are the constraints of the input? I mean are the values less than 10 only? Or it could be any large? – edusanketdk May 17 '21 at 03:13
  • EDITED the anwer now. – edusanketdk May 17 '21 at 03:34