0
Help with Safety deposit box in python
Having trouble with this activity about safety deposit boxes. Trying my best at python. It returns the index of the second test as 5 when I expect 1đ¤ Any advice will be appreciated. https://www.sololearn.com/coach/17?ref=app My code: items = input() box = input() index = items.index(box) time = (index + 1) * 5 print(time)
6 Answers
+ 2
Safety Deposit Boxes
You are robbing a bank, but youâre not taking everything. You are looking for a specific item in the safety deposit boxes and you are going to drill into each one in order to find your item. Once you find your item you can make your escape, but how long will it take you to get to that item?
Task
Determine the amount of time it will take you to find the item you are looking for if it takes you 5 minutes to drill into each box.
Input Format
A string that represent the items in each box that will be drilled in order (items are separated by a comma), and secondly, a string of which item you are looking for.
Output Format
An integer of the amount of time it will take for you to find your item.
Sample Input
'gold,diamonds,documents,Declaration of Independence,keys'
'Declaration of Independence'
Sample Output
20
+ 2
First my idea: â
.............................................................
item = input().split(",")
desire = input()
for i in item:
if i == desire:
print(5 * (item.index(i) + 1))
.............................................................
............................................................
Second pro idea:â
.............................................................
items = input().split(',')
desired = input()
time = 5 * (1 + items.index(desired))
print (time)
.............................................................
+ 2
Java version, it's works as well :
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String txt = scan.nextLine();
String find = scan.nextLine();
int count = 0;
//System.out.println(find);
for(String i: txt.split(",")){
if(i.equals(find)){
count+=5;
break;
}else{
count+=5;
continue;
}
}
System.out.println(count);
}
}
+ 2
Python version it's works too as well:
txt = str(input())
find = str(input())
txtSplit = txt.split(",")
count = 0
for i in txtSplit:
if i == find:
count = count +5
break
else:
count = count +5
continue
print(count)
0
my solution:
sth=input().split(',')
target=input()
print((sth.index(target)+1)*5)
=======================
â In my code, sth is a list.
Input>>
"gold,silver,jewels,cheese"
"silver"
sth=["gold", "silver", "jewels", "cheese"]
target="silver"
sth.index(target) #=1
-----------------------------------------------
list.index(element, start, end)
Parameters
⢠element - the element to be searched
⢠start (optional) - start searching from this index
⢠end (optional) - search the element up to this index
Return Value
⢠The index() method returns the index of the given element in the list.
⢠If the element is not found, a ValueError exception is raised.
Note: The index() method only returns the first occurrence of the matching element.
0
â In your code, items is a string.
Input>>
"gold,silver,jewels,cheese"
"silver"
           012345
items="gold,silver,jewels,cheese"
box="silver"
items.index(box)Â Â Â Â Â #=5
-----------------------------------------------
str.index(sub, beg, end)
Parameters
⢠sub â This specifies the substring to be searched.
⢠beg â This is the starting index, by default its 0.
⢠end â This is the ending index, by default its equal to the length of the string.
Return Value
âŞď¸ If substring exists inside the string, it returns the lowest index in the string where substring is found.
âŞď¸ If substring doesn't exist inside the string, it raises a ValueError exception.