Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

Arksiana7 avatar Arksiana7 26 Точки

3.Memory game - Mid Exam Retake - 12 August 2020

Здравейте!
Получавам 83 / 100. Всички тестове без последния минават. Явно изпускам някакъв случай и не мога да видя къде бъркам.

Кода ми: https://pastebin.com/hPQ00bpV
Линк към Judge: https://judge.softuni.bg/Contests/Practice/Index/2517#2

Ще съм благодарна за помоща Ви. 

 

03. Memory game

Write a program, which receives a sequence of elements. Each element in the sequence will have a twin. Until the player receives "end" from the console, he will receive strings with two integers separated by space, which represent the indexes of elements in the sequence. 

If the player tries to cheat and enters two equal indexes or indexes which are out of bounds of the sequence you should add two matching elements in the following format "-{number of moves until now}a" at the middle of the sequence and print this message on the console:

"Invalid input! Adding additional elements to the board"

Input

  • On the first line you will receive sequence of elements.

Output

  • Every time the player hit two matching elements you should remove them from the sequence and print on the console the following message:

"Congrats! You have found matching elements - ${element}!"

  • If the player hit two different elements, you should print on the console the following message:

"Try again!"

  • If the player hit all matching elements before he receives "end" from the console, you should print on the console the following message:

"You have won in {number of moves until now} turns!"

  • If the player receives "end" before he hits all matching elements, you should print on the console the following message:

"Sorry you lose :(

              {the current sequence's state}"

Constraints

  • All elements in the sequence will always have a matching element.

 

Examples

Input

Output

1 1 2 2 3 3 4 4 5 5

1 0

-1 0

1 0

1 0

1 0

end

Congrats! You have found matching elements - 1!

Invalid input! Adding additional elements to the board

Congrats! You have found matching elements - 2!

Congrats! You have found matching elements - 3!

Congrats! You have found matching elements - -2a!

Sorry you lose :(

4 4 5 5

Comment

1)

1 0

1 1 2 2 3 3 4 4 5 5 –> 1 = 1, equal elements, so remove them. Moves: 1

2)

-1 0

-1 is invalid index so we add additional elements

2 2 3 3 -2а -2а 4 4 5 5, Moves: 2

3)

1 0

2 2 3 3 -2а -2а 4 4 5 5 -> 2 = 2, equal elements, so remove them. Moves: 3

4)

1 0

3 3 -2а -2а 4 4 5 5 -> 3 = 3, equal elements, so remove them. Moves: 4

5)

1 0

-2а -2а 4 4 5 5 -> -2а = -2а, equal elements, so remove them. Moves: 5

6)

We receive end command.

There are still elements in the sequence, so we loose the game.

Final state - 4 4 5 5

a 2 4 a 2 4

0 3

0 2

0 1

0 1

end

Congrats! You have found matching elements - a!

Congrats! You have found matching elements - 2!

Congrats! You have found matching elements - 4!

You have won in 3 turns!

a 2 4 a 2 4

4 0

0 2

0 1

0 1

end

Try again!

Try again!

Try again!

Try again!

Sorry you lose :(

a 2 4 a 2 4

 

Тагове:
0
Python Fundamentals 11/11/2020 12:37:27
Axiomatik avatar Axiomatik 2422 Точки

From the JS-code that I have used, a winning condition is also obtained when only one element is remaining in the sequence, since no more double values are present :

If the player hit all matching elements before he receives "end"

Try to change lines 26-28 and include an additional validation when len(elements) == 1:

  1.  

      if len(elements) == 0:
    
            print(f"You have won in {moves} turns!")
    
            break

     

 

JS-variant:

 

    if (numbers.length === 0 || numbers.length === 1) {
        console.log(`You have won in ${moves} turns!`);
    }

 

Best,

 

 

0
11/11/2020 15:46:54
Arksiana7 avatar Arksiana7 26 Точки

Its a smart case, but still 83 / 100.

if len(elements) == 0 or len(elements) == 1:
    print(f"You have won in {moves} turns!")
    break

 

0
Axiomatik avatar Axiomatik 2422 Точки

Hmmmm,

try to change line 32 to include case when one element is still present, since otherwise you still print the "Sorry you lose :(" output, even though all double elements have been removed:

 if len(elements) >1 

 

 

0
Arksiana7 avatar Arksiana7 26 Точки

Umm ... nope. 

I was trying this: 

len(elements) > 1

and this ... 

len(elements) >= 1 

and still got 83 / 100. I think its something stupid that I cant see. 

Im about to cry :D 

Maybe judge hates me afrer all this tries. 

0
nd_nikolov avatar nd_nikolov 7 Точки

elements_sequence = input().split(" ")
# elements_sequence.remove("")
indexes = input()

count = 0

while indexes != "end":
    count += 1
    line = indexes.split(" ")

    if 0 > int(line[0]) or int(line[0]) > len(elements_sequence) - 1 or 0 > int(line[1]) or int(line[1]) > len(elements_sequence) - 1 or int(line[0]) == int(line[1]):
        print("Invalid input! Adding additional elements to the board")
        elements_sequence.insert(len(elements_sequence) // 2, f"-{count}a")
        elements_sequence.insert(len(elements_sequence) // 2, f"-{count}a")
    else:
        if int(line[0]) != int(line[1]) and elements_sequence[int(line[0])] == elements_sequence[int(line[1])]:
            print(f"Congrats! You have found matching elements - {elements_sequence[int(line[0])]}!")
            removed_element = elements_sequence[int(line[0])]
            elements_sequence.remove(removed_element)
            elements_sequence.remove(removed_element)
        elif elements_sequence[int(line[0])] != elements_sequence[int(line[1])] and int(line[0]) != int(line[1]):
            print("Try again!")
    if len(elements_sequence) == 0:
        print(f"You have won in {count} turns!")
        break
    indexes = input()
    if indexes == "end":
        print(f"Sorry you lose :(")
        print(" ".join(elements_sequence))
        break

0
nd_nikolov avatar nd_nikolov 7 Точки

Ако все още е актуална темата....

0
PetarMatev avatar PetarMatev 2 Точки

Подобно решение но малко по-кратко:

# 3 Memory Game:
sequence = input().split(" ")
counter = 0

while True:
    command = input()
    if command == "end":
        break
    counter += 1
    string = command.split(" ")
    left = int(string[0])
    right = int(string[1])
    if left == right or 0 > left or left >= len(sequence) or 0 > right or right >= len(sequence):
        print(f"Invalid input! Adding additional elements to the board")
        mid_point = len(sequence) // 2
        sequence.insert(mid_point, f"-{counter}a")
        sequence.insert(mid_point, f"-{counter}a")
    else:
        if sequence[left] == sequence[right]:
            left_item_to_remove = sequence[left]
            sequence.remove(left_item_to_remove)
            sequence.remove(left_item_to_remove)
            print(f"Congrats! You have found matching elements - {left_item_to_remove}!")
        else:
            print(f"Try again!")
    if len(sequence) == 0:
        print(f"You have won in {counter} turns!")
        break

if len(sequence) > 0:
    print(f"Sorry you lose :(")
    print(*sequence)
1
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.