Skip to content

Commit ccd69bf

Browse files
start adding bonus challenges and solutions notbook, list comprehension
1 parent 6b222f1 commit ccd69bf

File tree

3 files changed

+233
-5
lines changed

3 files changed

+233
-5
lines changed

Data_Sturctures.ipynb

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,81 @@
239239
],
240240
"id": "9d7a07cd1d46ae14"
241241
},
242+
{
243+
"metadata": {},
244+
"cell_type": "markdown",
245+
"source": [
246+
"## bonus challenge!! colinear line segments\n",
247+
"\n",
248+
"haii programmers!! it is time for bonus challenge ^w^ are you up to the task?? if you are not, then you're _very cringe_ and can move on to the next section. if you are up to it, then you're based :3c\n",
249+
"\n",
250+
"the bonus challenge is to write a function that takes in 2 points as tuples [(x1,y1,x2,y2), (x1,y1,x2,y2)] and returns whether the two line segments formed by those points are colinear! this means if the two line segments are parallel to each other then they're colinear. try using slope to determine if the two segments are colinear :3\n",
251+
"\n",
252+
"\n",
253+
"soooo you might be saying something likee, \"ohmygosh i failed gemoetry how do i do this help??\" but youre just overthinking itt just use point slope and its chill ^-^\n",
254+
"\n",
255+
"few things before you start, make sure to handle the case where the line segments are vertical and horizontal because that can cause some division errors! and don worry about the thing i put in the function definition and test cases if it doesnt error out then you did everything correct ^w^\n"
256+
],
257+
"id": "6553bb004c824e8a"
258+
},
259+
{
260+
"metadata": {
261+
"ExecuteTime": {
262+
"end_time": "2025-08-20T21:34:35.947772Z",
263+
"start_time": "2025-08-20T21:34:35.939974Z"
264+
}
265+
},
266+
"cell_type": "code",
267+
"source": [
268+
"# write your code here!!\n",
269+
"def are_colinear(point1:tuple[int,int,int,int], point2:tuple[int,int,int,int]) -> bool:\n",
270+
" # your code here!!\n",
271+
" pass\n",
272+
"\n",
273+
"# unit tests!\n",
274+
"# Test 1: Colinear horizontal segments\n",
275+
"assert are_colinear((0, 0, 1, 0), (2, 0, 3, 0)) == True\n",
276+
"\n",
277+
"# Test 2: Colinear vertical segments\n",
278+
"assert are_colinear((0, 0, 0, 1), (0, 2, 0, 3)) == True\n",
279+
"\n",
280+
"# Test 3: Colinear diagonal segments\n",
281+
"assert are_colinear((0, 0, 1, 1), (2, 2, 3, 3)) == True\n",
282+
"\n",
283+
"# Test 4: Non-colinear segments\n",
284+
"assert are_colinear((0, 0, 1, 2), (2, 0, 3, 3)) == False\n",
285+
"\n",
286+
"# Test 5: parallel horizontally\n",
287+
"assert are_colinear((0, 0, 1, 0), (0, 1, 1, 1)) == True\n",
288+
"\n",
289+
"assert are_colinear((-1,-1, 1,1), (-2,-1,0,2)) == False\n",
290+
"\n",
291+
"print(\"All unit tests passed!\")"
292+
],
293+
"id": "9373cc2d299f0214",
294+
"outputs": [
295+
{
296+
"name": "stdout",
297+
"output_type": "stream",
298+
"text": [
299+
"point1_slope: 0.0\n",
300+
"point2_slope: 0.0\n",
301+
"point1_slope: inf\n",
302+
"point2_slope: inf\n",
303+
"point1_slope: 1.0\n",
304+
"point2_slope: 1.0\n",
305+
"point1_slope: 2.0\n",
306+
"point2_slope: 3.0\n",
307+
"point1_slope: 0.0\n",
308+
"point2_slope: 0.0\n",
309+
"point1_slope: 1.0\n",
310+
"point2_slope: 1.5\n",
311+
"All unit tests passed!\n"
312+
]
313+
}
314+
],
315+
"execution_count": 6
316+
},
242317
{
243318
"metadata": {},
244319
"cell_type": "markdown",
@@ -389,7 +464,10 @@
389464
{
390465
"metadata": {},
391466
"cell_type": "markdown",
392-
"source": "## pratical exercise : TO WRITE",
467+
"source": [
468+
"## pratical exercise : TO WRITE\n",
469+
"\n"
470+
],
393471
"id": "95449c903c6b4dc1"
394472
}
395473
],

Loops_And_Logic.ipynb

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,96 @@
417417
"source": "However, lets say I only wanted the squares of the even numbers. I could add a condition to the list comprehension like this:",
418418
"id": "6460b1337be0f4da"
419419
},
420+
{
421+
"metadata": {
422+
"ExecuteTime": {
423+
"end_time": "2025-08-20T20:12:32.267621Z",
424+
"start_time": "2025-08-20T20:12:32.260021Z"
425+
}
426+
},
427+
"cell_type": "code",
428+
"source": [
429+
"even_squares = [x**2 for x in range(10) if x % 2 == 0]\n",
430+
"print(even_squares)\n",
431+
"\n",
432+
"odd_squares = [x**2 for x in range(10) if x % 2]\n",
433+
"print(odd_squares)"
434+
],
435+
"id": "22c54f87234a5eb2",
436+
"outputs": [
437+
{
438+
"name": "stdout",
439+
"output_type": "stream",
440+
"text": [
441+
"[0, 4, 16, 36, 64]\n",
442+
"[1, 9, 25, 49, 81]\n"
443+
]
444+
}
445+
],
446+
"execution_count": 7
447+
},
420448
{
421449
"metadata": {},
450+
"cell_type": "markdown",
451+
"source": [
452+
"Wondering why the second one works lmao? In Python, any non-zero number is considered `True`, and zero is considered `False`. So, when we use `if x % 2`, it evaluates to `True` for odd numbers (which give a remainder of 1 when divided by 2) and `False` for even numbers (which give a remainder of 0). the list comprehension `[x**2 for x in range(10) if x % 2]` filters out the even numbers and only includes the squares of the odd numbers.\n",
453+
"\n",
454+
"### some other handy oneliners\n",
455+
"\n",
456+
"if you want to apply a single function to all of a list, or even just part of a list you can do something like this:\n",
457+
"\n",
458+
"`[expression for item in items if condition]`\n",
459+
"\n",
460+
"If you want an if/else clause, put that first in the list comprehension like this:\n",
461+
"\n",
462+
"`[expression if condition else other_expression for item in items]`\n",
463+
"\n",
464+
"here's a few examples:"
465+
],
466+
"id": "b07daffde68e7df3"
467+
},
468+
{
469+
"metadata": {
470+
"ExecuteTime": {
471+
"end_time": "2025-08-20T21:01:31.683199Z",
472+
"start_time": "2025-08-20T21:01:31.675393Z"
473+
}
474+
},
422475
"cell_type": "code",
423-
"outputs": [],
424-
"execution_count": null,
425-
"source": "even_squares = [x**2 for x in range(10) if x % 2 == 0]\n",
426-
"id": "22c54f87234a5eb2"
476+
"source": [
477+
"words = [\"hello\", \"world\", \"this\", \"is\", \"a\", \"test\"]\n",
478+
"uppercased_words = [word.upper() for word in words]\n",
479+
"print(\" \".join(uppercased_words))\n",
480+
"\n",
481+
"inputs = [\"five\", \"seventy two\", None, \"twenty three\", \"forty two\"]\n",
482+
"inputs = [x for x in inputs if x is not None] # completely strip out the None values\n",
483+
"\n",
484+
"inputs = [\"five\", \"seventy two\", None, \"twenty three\", \"forty two\"]\n",
485+
"inputs = [x if x is not None else \"invalid\" for x in inputs] # replace None values with invalid"
486+
],
487+
"id": "6bece4dde8bc99c6",
488+
"outputs": [
489+
{
490+
"name": "stdout",
491+
"output_type": "stream",
492+
"text": [
493+
"HELLO WORLD THIS IS A TEST\n"
494+
]
495+
}
496+
],
497+
"execution_count": 8
498+
},
499+
{
500+
"metadata": {},
501+
"cell_type": "markdown",
502+
"source": "",
503+
"id": "4422e8750229c1c6"
504+
},
505+
{
506+
"metadata": {},
507+
"cell_type": "markdown",
508+
"source": "",
509+
"id": "9e42641679a367c9"
427510
}
428511
],
429512
"metadata": {

solutions.ipynb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "initial_id",
7+
"metadata": {
8+
"collapsed": true
9+
},
10+
"outputs": [],
11+
"source": [
12+
""
13+
]
14+
},
15+
{
16+
"metadata": {},
17+
"cell_type": "markdown",
18+
"source": [
19+
"## solution to data structures bonus challenge for tuples\n",
20+
"colinear line segments!!"
21+
],
22+
"id": "890d8ecdf5c15887"
23+
},
24+
{
25+
"metadata": {},
26+
"cell_type": "code",
27+
"outputs": [],
28+
"execution_count": null,
29+
"source": [
30+
"def are_colinear(point1:tuple[int,int,int,int], point2:tuple[int,int,int,int]) -> bool:\n",
31+
" x1,y1,x2,y2 = point1\n",
32+
" x3,y3,x4,y4 = point2\n",
33+
"\n",
34+
" point1_slope = (y2 - y1) / (x2 - x1) if (x2 - x1) != 0 else float('inf') # Handle vertical line case\n",
35+
" point2_slope = (y4 - y3) / (x4 - x3) if (x4 - x3) != 0 else float('inf')\n",
36+
"\n",
37+
" print(\"point1_slope:\", point1_slope)\n",
38+
" print(\"point2_slope:\", point2_slope)\n",
39+
" if point1_slope == point2_slope:\n",
40+
" return True\n",
41+
" return False"
42+
],
43+
"id": "cc0208171d3eaacd"
44+
}
45+
],
46+
"metadata": {
47+
"kernelspec": {
48+
"display_name": "Python 3",
49+
"language": "python",
50+
"name": "python3"
51+
},
52+
"language_info": {
53+
"codemirror_mode": {
54+
"name": "ipython",
55+
"version": 2
56+
},
57+
"file_extension": ".py",
58+
"mimetype": "text/x-python",
59+
"name": "python",
60+
"nbconvert_exporter": "python",
61+
"pygments_lexer": "ipython2",
62+
"version": "2.7.6"
63+
}
64+
},
65+
"nbformat": 4,
66+
"nbformat_minor": 5
67+
}

0 commit comments

Comments
 (0)