本文整理汇总了Python中linkedlist.LinkedList.append方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.append方法的具体用法?Python LinkedList.append怎么用?Python LinkedList.append使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类linkedlist.LinkedList
的用法示例。
在下文中一共展示了LinkedList.append方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_random_list
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
def create_random_list(self):
random_list = LinkedList()
x = random.randint(1, 200)
for i in range(0, x):
value = random.randint(1, 10000)
random_list.append(value)
return random_list
示例2: test_add
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
def test_add(self):
ll = LinkedList(4)
ll.append(6)
ll2 = LinkedList(10)
ll2.append(12)
ll2.append(13)
ll = ll + ll2
self.assertEqual(5, len(ll))
示例3: test_reverse
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
def test_reverse(self):
ll = LinkedList(7)
ll.append(8)
ll.append(9)
self.assertEqual(3, len(ll))
self.assertEqual(7, ll.peek())
self.assertEqual("7->8->9", str(ll))
ll.reverse()
self.assertEqual("9->8->7", str(ll))
self.assertEqual(9, ll.peek())
示例4: test_reverse_double
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
def test_reverse_double(self):
ll = LinkedList(45)
ll.append(46)
self.assertEqual(2, len(ll))
self.assertEqual(45, ll.peek())
self.assertEqual("45->46", str(ll))
ll.reverse()
self.assertEqual(2, len(ll))
self.assertEqual(46, ll.peek())
self.assertEqual("46->45", str(ll))
示例5: scan_file
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
def scan_file(self, file_name):
operations = LinkedList()
with open(file_name) as f:
i = 1
for line in f:
operation = self._parse_line(line)
if operation is not None:
operation.idx = i
i += 1
operations.append(operation)
return operations, self.src_regs
示例6: TestLinkedListReversal
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
class TestLinkedListReversal(unittest.TestCase):
def setUp(self):
self.testelements = [5, 3, 6, 7, 8, 10, 6]
self.expectedrev = [6, 10, 8, 7, 6, 3, 5]
self.linkedlist = LinkedList()
for e in self.testelements:
self.linkedlist.append(e)
def test_reversal(self):
self.linkedlist.reverse()
rev = [x for x in self.linkedlist]
self.assertItemsEqual(self.expectedrev, rev)
示例7: TestLinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
class TestLinkedList(unittest.TestCase):
def setUp(self):
self.list = LinkedList()
def test_element(self):
e = LinkedList.Element(self.list, 10)
self.assertEqual(e._list, self.list)
self.assertEqual(e.value, 10)
self.assertEqual(e.next, None)
def test_init(self):
self.assertEqual(self.list.head, None)
self.assertEqual(self.list.tail, None)
self.assertEqual(self.list.isEmpty, True)
# self.assertRaises(RuntimeError, self.list.first)
# self.assertRaises(RuntimeError, self.list.last)
def test_pend(self):
self.list.prepend(10)
self.assertEqual(self.list.head.value, 10)
self.list.prepend(11)
self.assertEqual(self.list.head.value, 11)
self.list.append(5)
self.assertEqual(self.list.last, 5)
self.assertEqual(self.list.isEmpty, False)
self.list.purge()
self.assertEqual(self.list.isEmpty, True)
def test_extract(self):
self.assertRaises(KeyError, self.list.extract, 10)
self.list.prepend(10)
self.list.prepend(5)
self.list.extract(10)
self.assertRaises(KeyError, self.list.extract, 6)
self.list.extract(5)
self.assertEqual(self.list.isEmpty, True)
def test_copy(self):
self.list.prepend(10)
self.list.prepend(5)
a = copy(self.list)
self.assertEqual(a.first, self.list.first)
self.assertEqual(a.last, self.list.last)
示例8: test_str_multiple_elements
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
def test_str_multiple_elements(self):
list = LinkedList(0)
list.append(1)
list.append(1)
list.append(2)
list.append(3)
list.append(5)
list.append(8)
self.assertEqual(str(list), '[0, 1, 1, 2, 3, 5, 8]')
示例9: sum_lists
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
def sum_lists(l, t):
node1 = l.head
node2 = t.head
carry = 0
result = LinkedList()
while node1 is not None or node2 is not None:
first_parcel = 0
second_parcel = 0
if node1 is not None:
first_parcel = node1.value
node1 = node1.next
if node2 is not None:
second_parcel = node2.value
node2 = node2.next
sum_value = first_parcel + second_parcel + carry
result.append(sum_value % 10)
carry = sum_value/10
if carry > 0:
result.append(carry)
return result
示例10: test_append
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
def test_append(self):
"""Append to list at the top, append in the middle, and append bad items."""
list1 = LinkedList()
list2 = LinkedList(1, 2, 3)
# Append to empty list at head position continuously.
list1.append(0)
list1.append(0, 0)
list1.append(0, 1)
assert list1 == LinkedList(0, 0, 0)
list2.append(4, 1)
assert list2 == LinkedList(1, 2, 4, 3)
for bad_index in [-1, 0.2, 5, "foo"]:
try:
list2.append(5, bad_index)
assert False
except (IndexError, ValueError):
assert True
示例11: main
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
def main():
""" don't modify this function. even if you do I have a copy of the original to test with """
mylist = LinkedList()
# add some initial nodes
mylist.append(10)
mylist.append(20)
mylist.append(30)
# this should display 10 20 30
print(mylist)
# add some nodes to the front
mylist.prepend(0)
mylist.prepend(-10)
# this should display: -10 0 10 20 30
print(mylist)
mylist.update(0, -20) # change -10 to -20
mylist.insert(2, 5) # insert 5 between 0 and 10
mylist.insert(5, 25) # insert 25 between 20 and 30
mylist.remove(1) # remove the 0
# this should display: -20 5 10 20 25 30
print(mylist)
mylist.remove(3) # removes 20
mylist.append(40) # add 40 to the end
mylist.append(50) # add 50 to the end
mylist.update(5, 45) # update 40 to 45
mylist.remove(5) # remove 45
mylist.update(5, 55) # update 50 to 55
mylist.prepend(-30) # add -30 to the front
# this should display -30 -20 5 10 25 30 55
print(mylist)
示例12: Test_LinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
class Test_LinkedList(unittest.TestCase):
def setUp(self):
self.ll = LinkedList(4)
self.ll.prepend(5)
def test_simple(self):
self.assertEqual(5, self.ll.peek())
self.assertEqual(2, len(self.ll))
def test_prepend(self):
ll = LinkedList(3)
self.assertEqual(1, len(ll))
self.assertEqual(3, ll.peek())
ll.prepend(2)
self.assertEqual(2, len(ll))
self.assertEqual(2, ll.peek())
ll.prepend(1)
self.assertEqual(3, len(ll))
self.assertEqual(1, ll.peek())
def test_append(self):
self.ll.append(6)
self.assertEqual(5, self.ll.peek())
self.assertEqual(3, len(self.ll))
self.assertEqual(4, self.ll.next.peek())
self.assertEqual(6, self.ll.next.next.peek())
def test_len(self):
self.ll.append(6)
self.assertEqual(3, len(self.ll))
def test_len_uninitialized(self):
ll = LinkedList()
ll.prepend(5)
self.assertEqual(1, len(ll))
def test_pop(self):
head_value = self.ll.pop()
self.assertEqual(5, head_value)
self.assertEqual(1, len(self.ll))
self.assertEqual(4, self.ll.peek())
def test_pop_to_empty(self):
ll = LinkedList(5)
self.assertEqual(1, len(ll))
self.assertEqual(ll.next, None)
self.assertFalse(ll.isEmpty())
head_value = ll.pop()
self.assertEqual(head_value, 5)
self.assertEqual(0, len(ll))
self.assertEqual(ll.next, None)
self.assertTrue(ll.isEmpty())
def test_isEmpty(self):
ll = LinkedList()
self.assertTrue(ll.isEmpty())
def test_pop_empty(self):
ll = LinkedList()
self.assertRaises(ListEmptyError, ll.pop)
def test_add(self):
ll = LinkedList(4)
ll.append(6)
ll2 = LinkedList(10)
ll2.append(12)
ll2.append(13)
ll = ll + ll2
self.assertEqual(5, len(ll))
def test_clear(self):
self.assertFalse(self.ll.isEmpty())
self.assertTrue(len(self.ll) == 2)
self.ll.clear()
self.assertTrue(self.ll.isEmpty())
def test_clear_empty_list(self):
ll = LinkedList()
ll.clear()
self.assertTrue(ll.isEmpty())
def test_reversed(self):
ll = LinkedList(7)
ll.append(8)
ll.append(9)
self.assertEqual(3, len(ll))
self.assertEqual(7, ll.peek())
self.assertEqual("7->8->9", str(ll))
rev = ll.reversed()
self.assertEqual("9->8->7", str(rev))
self.assertEqual(9, rev.peek())
self.assertEqual(3, len(ll))
self.assertEqual(7, ll.peek())
self.assertEqual("7->8->9", str(ll))
def test_reverse(self):
ll = LinkedList(7)
ll.append(8)
ll.append(9)
self.assertEqual(3, len(ll))
#.........这里部分代码省略.........
示例13: TestLineSegment
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
class TestLineSegment(unittest.TestCase):
def setUp(self):
self.q = LinkedList()
self.q.append(5)
self.q.append(10)
self.q.append(15)
self.q.append(20)
self.q.append(25)
def testLen(self):
l = len(self.q)
self.assertEquals(l, 5,
"len(): expected length of 5 but got {}".format(l))
def testPosIndex(self):
e = self.q[2]
self.assertEquals(e, 15,
"positive indexing: expected 15 but got {}".format(e))
def testZeroIndex(self):
e = self.q[0]
self.assertEquals(e, 5,
"indexing: sliced index 0. expected 5 but got {}".format(e))
def testAppend(self):
self.q.append("Bob")
l = len(self.q)
last = self.q[5]
self.assertEquals(l, 6,
"appending: expected new list length 6 but got {}".format(l))
self.assertEquals(last, "Bob",
"appending: expected \"Bob\" element but got {}".format(last))
def testNegIndex(self):
e = self.q[-2]
self.assertEquals(e, 20,
"negative indexing: expected 20, but got {}".format(e))
def testHighIndex(self):
try:
self.q[99]
self.assertEquals(True, True,
"positive indexing: expected IndexError")
except IndexError as e:
pass
except:
self.assertEquals(True, True,
"positive indexing: expected IndexError, got {}".format(e))
def testLowIndex(self):
try:
self.q[-100]
self.assertEquals(True, True,
"negative indexing: expected IndexError")
except IndexError as e:
pass
except:
self.assertEquals(True, True,
"negative indexing: expected IndexError, got {}".format(e))
def testWeirdIndex(self):
try:
self.q["Bob"]
self.assertEquals(True, True,
"negative indexing: expected IndexError")
except IndexError as e:
pass
except Exception as e:
self.assertEquals(True, True,
"negative indexing: expected IndexError, got {}".format(e))
def testString(self):
s = str(self.q).replace(' ', '')
self.assertEquals(s, "[5,10,15,20,25]",
"printing/str(): expected [5,10,15,20,25], but got {}".format(s))
def testConstructByList(self):
q2 = LinkedList([2, 4, 6, 8, 10, 12])
l = len(q2)
self.assertEquals(l, 6,
"constructing by list: expected length of 6, got {}".format(l))
def testSlice(self):
self.assertEquals(self.q[2:4][0], 15,
"slicing region: expected [15,20], got {}".format(self.q[2:4]))
self.assertEquals(self.q[2:4][1], 20,
"slicing region: expected [15,20], got {}".format(self.q[2:4]))
def testSliceReturnType(self):
t = type(self.q[2:4])
isLinkedList = t is LinkedList
self.assertEquals(isLinkedList, True,
"slicing region: expected an instance of LinkedList, got {}".format(t))
def testSetValue(self):
self.q[1] = "Alfred"
e = self.q[1]
self.assertEquals(e, "Alfred",
"setting: expected \"Alfred\", got {}".format(e))
#.........这里部分代码省略.........
示例14: question4
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
0,
0,
0)
#Should print 0
print question4([[0, 1, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 0, 0, 0, 1],
[0, 0, 0, 0, 0]],
0,
911,
-4)
print "*******************************************"
print
print
print "TEST CASES FOR QUESTION 5"
print "*******************************************"
ll = LinkedList(Node(1))
for i in range(2,10):
ll.append(Node(i))
#should print 7
print question5(ll,3)
#should print None
print question5(ll,0)
#should print None
print question5(ll,500)
print "*******************************************"
print
示例15: TestLinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import append [as 别名]
class TestLinkedList(unittest.TestCase):
def setUp(self):
"""Test setup"""
self.empty_list = LinkedList()
self.A_LIST_HEAD = 42
self.a_list = LinkedList(self.A_LIST_HEAD)
def test_new_list(self):
"""A criação de uma Nova Lista"""
self.assertIsInstance(self.empty_list, LinkedList)
def test_new_list_empty(self):
"""Uma Nova Lista criada sem parâmetros é vazia"""
self.assertTrue(self.empty_list.is_empty())
def test_empty_list_equality(self):
"""Listas Vazias são iguais"""
a = LinkedList()
b = LinkedList()
self.assertEqual(self.empty_list, a)
self.assertEqual(self.empty_list, b)
self.assertEqual(a, b)
def test_new_list_not_empty_with_value(self):
"""Uma Nova Lista criada com parâmetros é não-vazia"""
self.assertFalse(self.a_list.is_empty())
def test_empty_list_size(self):
"""Uma Lista vazia possui tamanho zero"""
self.assertEqual(self.empty_list.size(), 0)
def test_a_list_with_one_element_size_is_one(self):
"""Uma Lista com um elemento possui tamanho 1"""
self.assertEqual(self.a_list.size(), 1)
def test_a_list_head_is_the_first_element(self):
"""A cabeça de uma lista é seu primeiro elemento"""
"""Caso a lista seja vazia, o valor da cabeça é None"""
self.assertEqual(self.empty_list.head().value,None)
self.assertEqual(self.a_list.head().value, self.A_LIST_HEAD)
def test_a_list_tail_is_the_rest_of_the_list(self):
"""A cauda de uma lista são todos os elementos menos a cabeça"""
"""Caso a lista seja vazia ou tenha somente um elemento,
a Cauda é uma Lista Vazia"""
self.assertEqual(self.empty_list.tail(),LinkedList())
self.assertEqual(self.a_list.tail(), LinkedList() )
def test_append_changes_list_size(self):
"""Append insere um elemento no final da lista"""
"""A operação append aumenta o tamanho da lista em 1"""
size = self.a_list.size()
self.a_list.append(55)
new_size = size + 1
self.assertEqual(self.a_list.size(), new_size)
def test_append_changes_list_size_multiple_elements(self):
"""X appends aumentam o tamanho da lista em X elementos"""
size = self.a_list.size()
x = random.randint(1, 200)
for i in range(0, x):
value = random.randint(1, 10000)
self.a_list.append(value)
new_size = size + x
self.assertEqual(self.a_list.size(), new_size)
def test_append_empty_list_appended_element_is_head(self):
"""Append numa lista vazia torna o novo elemento a cabeça da lista"""
self.assertTrue(self.empty_list.is_empty())
self.assertTrue(self.empty_list.head().is_empty())
x = 42
self.empty_list.append(x)
self.assertEqual(self.empty_list.head().value, x)
def test_prepend_changes_list_size(self):
"""Prepend insere um elemento no início da lista"""
"""A operação prepend aumenta o tamanho da lista em 1"""
size = self.a_list.size()
self.a_list.prepend(98)
new_size = size + 1
self.assertEqual(self.a_list.size(), new_size)
def test_prepend_changes_list_size_multiple_elements(self):
"""X prepends aumentam o tamanho da lista em X elementos"""
size = self.a_list.size()
x = random.randint(1, 200)
#.........这里部分代码省略.........