Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 541065f

Browse files
task 1
1 parent a762765 commit 541065f

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

‎0x0A_CPython/0-python_lists_bigO‎

100644100755
File mode changed.

‎0x0A_CPython/1-python.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <Python.h>
2+
#include <stdio.h>
3+
4+
/**
5+
* print_python_list - print some basic info about Python lists
6+
* @p: python object
7+
**/
8+
void print_python_list(PyObject *p)
9+
{
10+
Py_ssize_t listsize, i;
11+
PyObject *item;
12+
13+
printf("[*] Python list info\n");
14+
if (!PyList_Check(p))
15+
printf("Invalid List Object\n");
16+
else
17+
{
18+
listsize = ((PyVarObject *) p)->ob_size;
19+
printf("[*] Size of the Python List = %lu\n", listsize);
20+
printf("[*] Allocated = %lu\n",
21+
((PyListObject *) p)->allocated);
22+
for (i = 0; i < listsize; i++)
23+
{
24+
item = ((PyListObject *) p)->ob_item[i];
25+
printf("Element %lu: %s\n", i, item->ob_type->tp_name);
26+
}
27+
}
28+
}

‎0x0A_CPython/README.md‎

100644100755
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,17 @@
1414
* L10 - Sorting the list. Example: holberton.sort()
1515
* L11 - Reversing the list. Example: holberton.reverse()
1616
* L12 - Deleting the list. Example: del holberton
17+
18+
19+
* 1-python.c - prints basic info about python lists
20+
21+
22+
## Useful Links:
23+
24+
* [listobject.h](https://github.com/python/cpython/blob/master/Include/listobject.h)
25+
26+
* [object.h](https://github.com/python/cpython/blob/master/Include/object.h)
27+
28+
* [Common Object Structures](https://docs.python.org/3.4/c-api/structures.html)
29+
30+
* [List Objects](https://docs.python.org/3.4/c-api/list.html)

‎0x0A_CPython/tests/1-test_lists.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import ctypes
2+
3+
lib = ctypes.CDLL('./libPyList.so')
4+
lib.print_python_list.argtypes = [ctypes.py_object]
5+
l = ['hello', 'World']
6+
lib.print_python_list(l)
7+
del l[1]
8+
lib.print_python_list(l)
9+
l = l + [4, 5, 6.0, (9, 8), [9, 8, 1024], "Holberton"]
10+
lib.print_python_list(l)
11+
l = []
12+
lib.print_python_list(l)
13+
l.append(0)
14+
lib.print_python_list(l)
15+
l.append(1)
16+
l.append(2)
17+
l.append(3)
18+
l.append(4)
19+
lib.print_python_list(l)
20+
l.pop()
21+
lib.print_python_list(l)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /