GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — tech-code-interview/house_robber.py at master · BarthJr/tech-code-interview

Expand file tree

/

house_robber.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
46 lines (33 loc) · 1.48 KB
/

house_robber.py

Copy path

File metadata and controls

46 lines (33 loc) · 1.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# You are a professional robber planning to rob houses along a street.
# Each house has a certain amount of money stashed,
# the only constraint stopping you from robbing each of them is that adjacent houses have security system connected
# and it will automatically contact the police if two adjacent houses were broken into on the same night.
# Given a list of non-negative integers representing the amount of money of each house,
# determine the maximum amount of money you can rob tonight without alerting the police.
from typing import List
from unittest import TestCase
def rob(nums: List[int]) -> int:
memo = [None] * (len(nums) + 1)
return _rob(0, nums, memo)
def _rob(i, nums, memo):
if i > len(nums) - 1:
return 0
else:
if memo[i] is not None:
return memo[i]
result = max(nums[i] + _rob(i + 2, nums, memo), _rob(i + 1, nums, memo))
memo[i] = result
return result
class TestRob(TestCase):
def test_with_empty_list(self):
self.assertEqual(0, rob([]))
def test_with_list_zeros(self):
self.assertEqual(0, rob([0, 0, 0, 0]))
def test_with_one_element(self):
self.assertEqual(2, rob([2]))
def test_with_two_elements_and_last_bigger(self):
self.assertEqual(2, rob([1, 2]))
def test_with_two_elements_and_first_bigger(self):
self.assertEqual(2, rob([2, 1]))
def test_if_not_return_max_profit_with_adjacent_houses(self):
self.assertEqual(4, rob([1, 2, 3, 1]))
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.