Python Forum
Problem with "Number List" problem on HackerRank
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with "Number List" problem on HackerRank
#5
Here is a much shorter attempt
def solve(L, K):
    n, s, p = len(L), 0, -1
    for i, x in enumerate(L):
        if x > K:
            s += (i - p) * (n - i)
            p = i
    return s

inp = """\
2
3 2
1 2 3 
3 1
1 2 3
"""

def main():
    import io
    f = io.StringIO(inp)
    ntcase = int(next(f).strip())
    for i in range(ntcase):
        N, K = (int(x) for x in next(f).strip().split())
        L = [int(x) for x in next(f).strip().split()]
        assert(len(L) == N)
        print(solve(L, K))

if __name__ == '__main__':
    main()
Reply


Messages In This Thread
RE: Number List problem from HackerRank - by Pnerd - Apr-09-2022, 12:40 AM
RE: Problem with "Number List" problem on HackerRank - by Gribouillis - Apr-10-2022, 08:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  element in list detection problem jacksfrustration 5 435 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  list in dicitonary element problem jacksfrustration 3 752 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 739 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,584 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,285 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List Sorting Problem ZZTurn 5 1,385 Sep-22-2022, 11:23 PM
Last Post: ZZTurn
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,930 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,338 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,100 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 3,154 Feb-03-2022, 03:47 PM
Last Post: Frankduc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020