Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit dc223c9

Browse files
python: add problem 61 and unittest
1 parent 08c284b commit dc223c9

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

Python/sln_1_100/solution_61_70.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
# -*- coding: utf-8 -*-
2+
from common_utils import *
3+
24

35
class Solution_61_70(object):
6+
def rotateRight(self, head, k):
7+
"""
8+
61
9+
:param head:
10+
:param k:
11+
:return:
12+
"""
13+
if not head or not head.next:
14+
return head
15+
16+
list_len = 1
17+
18+
node = head
19+
while node.next:
20+
list_len += 1
21+
node = node.next
22+
last_node = node
23+
node = head.next
24+
prev_node = head
25+
if k % list_len == 0:
26+
new_head = head
27+
else:
28+
for _ in range(list_len - k % list_len - 1):
29+
prev_node = prev_node.next
30+
node = node.next
31+
new_head = node
32+
prev_node.next = None
33+
last_node.next = head
34+
return new_head
35+
436
def addBinary(self, a, b):
537
"""
638
67
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
# -*- coding: utf-8 -*-
22
from sln_1_100.solution_61_70 import Solution_61_70
3+
from common_utils import *
34
from unittest import TestCase
5+
6+
47
class TestSolution_61_70(TestCase):
58
def setUp(self):
69
self.sln = Solution_61_70()
710

11+
def test_rotate_right(self):
12+
l1 = ListNode(1)
13+
l2 = ListNode(2)
14+
l1.next = l2
15+
l3 = ListNode(3)
16+
l2.next = l3
17+
l4 = ListNode(4)
18+
l3.next = l4
19+
l5 = ListNode(5)
20+
l4.next = l5
21+
node = self.sln.rotateRight(l1, 8)
22+
while node:
23+
print(node.val)
24+
node = node.next
25+
826
def test_addBinary(self):
9-
ret = self.sln.addBinary('11','1')
10-
print(ret)
27+
ret = self.sln.addBinary('11', '1')
28+
print(ret)

0 commit comments

Comments
 (0)