Skip to content

Commit b92935f

Browse files
committed
0x03-python-data_structures
1 parent 1677742 commit b92935f

17 files changed

+241
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/python3
2+
def print_list_integer(my_list=[]):
3+
for i in range(len(my_list)):
4+
print("{:d}".format(my_list[i]))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/python3
2+
3+
def element_at(my_list, idx):
4+
if (idx < 0) or (idx > len(my_list) - 1):
5+
return None
6+
return(my_list[idx])
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/python3
2+
3+
4+
def divisible_by_2(my_list=[]):
5+
"""Find all multiples of 2 in a list."""
6+
multiples = []
7+
for i in range(len(my_list)):
8+
if my_list[i] % 2 == 0:
9+
multiples.append(True)
10+
else:
11+
multiples.append(False)
12+
13+
return (multiples)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <Python.h>
2+
3+
/**
4+
* print_python_list_info - Prints basic info about Python lists.
5+
* @p: A PyObject list.
6+
*/
7+
void print_python_list_info(PyObject *p)
8+
{
9+
int size, alloc, i;
10+
PyObject *obj;
11+
12+
size = Py_SIZE(p);
13+
alloc = ((PyListObject *)p)->allocated;
14+
15+
printf("[*] Size of the Python List = %d\n", size);
16+
printf("[*] Allocated = %d\n", alloc);
17+
18+
for (i = 0; i < size; i++)
19+
{
20+
printf("Element %d: ", i);
21+
22+
obj = PyList_GetItem(p, i);
23+
printf("%s\n", Py_TYPE(obj)->tp_name);
24+
}
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/python3
2+
3+
4+
def delete_at(my_list=[], idx=0):
5+
"""Delete an item at a specific position in a list."""
6+
if idx >= 0 and idx < len(my_list):
7+
del my_list[idx]
8+
return (my_list)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/python3
2+
a = 89
3+
b = 10
4+
a, b = b, a
5+
print("a={:d} - b={:d}".format(a, b))
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "lists.h"
2+
3+
listint_t *reverse_listint(listint_t **head);
4+
int is_palindrome(listint_t **head);
5+
6+
/**
7+
* reverse_listint - Reverses a singly-linked listint_t list.
8+
* @head: A pointer to the starting node of the list to reverse.
9+
*
10+
* Return: A pointer to the head of the reversed list.
11+
*/
12+
listint_t *reverse_listint(listint_t **head)
13+
{
14+
listint_t *node = *head, *next, *prev = NULL;
15+
16+
while (node)
17+
{
18+
next = node->next;
19+
node->next = prev;
20+
prev = node;
21+
node = next;
22+
}
23+
24+
*head = prev;
25+
return (*head);
26+
}
27+
28+
/**
29+
* is_palindrome - Checks if a singly linked list is a palindrome.
30+
* @head: A pointer to the head of the linked list.
31+
*
32+
* Return: If the linked list is not a palindrome - 0.
33+
* If the linked list is a palindrome - 1.
34+
*/
35+
int is_palindrome(listint_t **head)
36+
{
37+
listint_t *tmp, *rev, *mid;
38+
size_t size = 0, i;
39+
40+
if (*head == NULL || (*head)->next == NULL)
41+
return (1);
42+
43+
tmp = *head;
44+
while (tmp)
45+
{
46+
size++;
47+
tmp = tmp->next;
48+
}
49+
50+
tmp = *head;
51+
for (i = 0; i < (size / 2) - 1; i++)
52+
tmp = tmp->next;
53+
54+
if ((size % 2) == 0 && tmp->n != tmp->next->n)
55+
return (0);
56+
57+
tmp = tmp->next->next;
58+
rev = reverse_listint(&tmp);
59+
mid = rev;
60+
61+
tmp = *head;
62+
while (rev)
63+
{
64+
if (tmp->n != rev->n)
65+
return (0);
66+
tmp = tmp->next;
67+
rev = rev->next;
68+
}
69+
reverse_listint(&mid);
70+
71+
return (1);
72+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/python3
2+
3+
4+
def replace_in_list(my_list, idx, element):
5+
if (idx < 0) or (idx > len(my_list) - 1):
6+
return my_list
7+
else:
8+
my_list[idx] = element
9+
return my_list
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/python3
2+
3+
4+
def print_reversed_list_integer(my_list=[]):
5+
if isinstance(my_list, list):
6+
my_list.reverse()
7+
for i in my_list:
8+
print("{:d}".format(i))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/python3
2+
def new_in_list(my_list, idx, element):
3+
if (idx < 0 or len(my_list) <= idx):
4+
return my_list
5+
newList = my_list[:]
6+
newList[idx] = element
7+
return newList

0 commit comments

Comments
 (0)