Generation

generate functionTue, 08 Nov 2022

You are given a string representing a sequence of N arrows, each pointing in one of the four cardinal directions: up ('^), down (v'), left (*) or right (>'). Write a function solution that, given a string S denoting the directions of the arrows, returns the minimum number of arrows that must be rotated to make them all point in the same direction. Assume that: N is an integer within the range [1..100); string S is made only of the following characters: "^" "v". "<" and/or"s Example: Given S= "v>>>w"

def solution(S): # write your code in Python 3.6 up = 0 down = 0 left = 0 right = 0 for i in S: if i == "v": down += 1 if i == "^": up += 1 if i == "<": left += 1 if i == ">": right += 1 return min(left, right) + min(up, down)

Want to kickstart your project?Use the new AI Studio to create your code