From f614b323b56197cf121080e24d11d6e1b523ce8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iida=20Pyykk=C3=B6nen?= <iapyyk@utu.fi> Date: Mon, 20 Jan 2025 14:51:42 +0200 Subject: [PATCH] 18 and 20 --- 18.py | 17 +++++++++++++++++ 20.py | 15 +++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 18.py create mode 100644 20.py diff --git a/18.py b/18.py new file mode 100644 index 0000000..93187e2 --- /dev/null +++ b/18.py @@ -0,0 +1,17 @@ +# Write a function solve that generates the first n rows of Pascal's Triangle. + +def solve(rows): + triangle = [] + for i in range(0, rows): + row = [] + row.append(1) + if i == 1: + row.append(1) + elif i > 1: + for j in range(0, i-1): + first = triangle[i-1][j] + second = triangle[i-1][j+1] + row.append(first + second) + row.append(1) + triangle.append(row) + return triangle diff --git a/20.py b/20.py new file mode 100644 index 0000000..6747417 --- /dev/null +++ b/20.py @@ -0,0 +1,15 @@ +# Write a function that, given the head of a singly linked list, reverses the list. +# Return the root node of the reversed list. + +def solve(head): + current = head + prev = None + + while current: + temp_next = current.next + current.next = prev + + prev = current + current = temp_next + return prev + -- GitLab