-2

Ok so here is the code. I dont plan on typing out the error in the log. Its not needed to copy the code. Its basically saying that there is an issue where 'L' is not type int.

def i_am_here(path):
  print(path)
  lst = []
  num = []
  x = 0
  y = 0
  for i in path:
    try: 
        int(i)
        num.append(int(i))
    except ValueError:
        if i == 'r' or i == 'R':
            new = ''.join(num)
            lst.append(new)
            lst.append('r')
            num = []
        if i == 'L' or i == 'l':
            new = ''.join(num)
            lst.append(new)
            lst.append('l')
            num = []
  new = ''.join(num)
  lst.append(new)
  lst = lst[1:len(lst)]
  print(lst)
  for i in range(len(lst)):

    if lst[i] == 'r':
        print(lst[i+1])

enter image description here

ok, so im 99% sure this is just a bug as when I use:

for i in path:
    try: 
        int(i)
        num.append(int(i))
    except ValueError:
        if i == 'r' or i == 'R':

            lst.append(num)
            lst.append('r')
            num = []
        if i == 'L' or i == 'l':

            lst.append(num)
            lst.append('l')
            num = []

everything works just fine. Any thoughts? I was going to submit an issue ticket but before I did I thought I might ask.

2 Answers2

1

It is connected with parameter path that you try to send to function i_am_here. It you try to send the string with a single char then there is no problem. Problem arise with integer value.

If you try to call function using a way:

i_am_here(1)

you have got the problem here

for i in path:

because in for cycle you cannot use simple integer value, only iterable objects such as lists or strings.

Instead of using single integer value you should use a list:

param_lst = [1]

i_am_here(param_lst)

And also you should fix your code here:

new = ''.join(num)

You should rewrite it to:

new = ''.join(str(n) for n in num)
Mikhail Kanin
  • 11
  • 1
  • 2
  • the path will always be inputed as a string or empty. thats how its inputed. id love for it to already be in a list form, buts thats just how they made the challenge...? I'm confused by your answer. –  Dec 02 '18 at 23:49
0

Your code broken down because it's trying to convert a string with non numeric characters, for example:

In:

text = 'L'
print int(text)

Out:

ValueError: invalid literal for int() with base 10: 'L'

But if:

In:

text = '2'
print int(text)

Out:

2

However, I think the most lines of your code are useless. I just tied to remove those parts. And here it's:

Edit:

def i_am_here(path):
    num = []
    for i in path:
        if isinstance(i, str): #check if 'i' is a sting type
          if i.isdigit():
             num.append(int(i))
          else:  
             num.append(i.lower())
        else:
           num.append(i)
    return(num)

For example:

path= [5, 'r', 'L', 0.00032,'l','55','%','R', [], '{}'] #This is an example
num_List = i_am_here(path)
for i in num_List :
        if i != 'r':
            print(i) 

You're literally trying to convert upercase strings to lowercase.

Kian
  • 1,319
  • 1
  • 13
  • 23
  • Isnt that the whole point of a try: except: function? to take the error that I know is being thrown for "r", "R", "l", "L". and move on the the except function? –  Dec 02 '18 at 23:32
  • also yes your right I'm going around the a** to get to the elbow. So thanks. I just kinda started typing it out and not thinking about efficiency. Usually i come back and look for speed after. thanks! –  Dec 02 '18 at 23:37
  • there is only one issue with this mode of doing things what happens when I need to go 55 right? –  Dec 02 '18 at 23:41
  • also last thing. all types will be str. they are inputed as such so even though its 5 its actually '5' which should be true right? or am i mistaken on that? –  Dec 02 '18 at 23:54
  • Technically If an error is encountered, a try block code execution is stopped and transferred down to the except block. You can add `try: except` statement to your code, however I think you don't need it because I already added various conditions so in the worst case you will have your **path_list** elements as it is(if they are not in the scope of if:else conditions) – Kian Dec 03 '18 at 09:06
  • Regarding to your last question, `print type('5')` gives me **** and `print type(5)` gives ****, therefore their types are different! However in code there is a condition which converts '55' as an integer;like `int('55') `; so in final list whether the str is '55' or 55 you will have 55 (which is an integer) – Kian Dec 03 '18 at 09:22