#!/bin/python3 import math import os import random import re import sys # # Comp
ID: 3753748 • Letter: #
Question
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'powerJump' function below.
#
# The function is expected to return an INTEGER.
# The function accepts STRING game as parameter.
#
def powerJump(game):
In a revised game of hopscotch, a child is trying to cross a line of tiles with a binary string painted on it. Consider the line of tiles to be like a 1D array, where each tile has either a 1 or a O, and a consecutive series of tiles makes the whole string. The game starts with the child standing in front of the leftmost character of the string. All jumps to reach the end of the string can only be on tiles with 0 or only be on tiles with 1 The game is won if the child can reach the end of the string, taking jumps with the minimum required power. The power of a jump is given by the number of tiles in the path of a jump as indicated in the diagram below. In 10101, the power of the jump from beginning to the first tile is 1, but from the first to the third tile is 2, and so on. Find the minimum power the child's jump should have in order to win the game for different binary strings. Note: The value on the last tile determines which tiles to jump on. This is because all tiles landed on must have the same value and the child must land on the last tile to complete the game. In one jump, the child can jump to the right, any distance from 1 to the value of the power of her jump. For example, for the string 10101, the power of the jump needs to be minimum 2. power of the jump 1 power of the jump-2 power of the jump 2 1 0 1 0 1Explanation / Answer
import math
import os
import random
import re
import sys
def powerJump(game):
lastChar = game[len(game) - 1]
index = game.find(lastChar) #find the value of the last tile
maxJump = index + 1 #maximum power of the jump initialized to the first jump
while index < len(game) - 1:
nextIndex = game.find(lastChar, index + 1, len(game)) #index of the next tile to jump on
if nextIndex - index > maxJump:
maxJump = nextIndex - index #if next jump is bigger than the current max power of the jump, then update max power of the jump
index = nextIndex
return maxJump
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.