Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save e3krisztian/aa00799841e7869bb8ed to your computer and use it in GitHub Desktop.
Save e3krisztian/aa00799841e7869bb8ed to your computer and use it in GitHub Desktop.
class4 tasks: loops and strings
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Class4 hexdump"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Ezt egyszer le kell futtatni, hogy legyen `expect()`:\n",
"\n",
"különben\n",
"\n",
" NameError: name 'expect' is not defined\n",
"-et kapunk"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# assert helyett - ez kiírja a várt értéket és a kapott eredményt\n",
"\n",
"def expect(result, actual_result):\n",
" assert actual_result == result, (\n",
" 'Got %r instead of %r' % (actual_result, result))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Teszt az `expect()`-hez"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# tohex2 - házi megoldása, hosszan kiírva az érthetőség miatt\n",
"# (egy sorba is elférne, de nehezen érthetően)\n",
"def tohex2(byte):\n",
" '''\n",
" Convert byte to two digit hexadecimal, uppercase if digit > 9\n",
" '''\n",
" hex_byte = hex(byte)\n",
" digits = hex_byte[2:]\n",
" padded_digits = ('0' + digits)[-2:]\n",
" uppercase_digits = padded_digits.upper()\n",
" return uppercase_digits\n",
"\n",
"# Működik az expect? vagy\n",
"# NameError: name 'expect' is not defined\n",
"expect('00', tohex2(0))\n",
"expect('0F', tohex2(15))\n",
"expect('FF', tohex2(255))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## tohex2: a gyakorlatban string formázással konvertálunk\n",
"\n",
"Amiből kétféle van:\n",
"\n",
"- `'régi %s' % 'sablon'`\n",
"- `'új {}'.format('sablon')`\n",
"\n",
"mindkettő él, működik, kb. ugyanazt tudja.\n",
"\n",
"Részletesebben: https://docs.python.org/3.1/library/string.html#formatstrings\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"régi format: 0B\n",
"új format: 1F\n"
]
}
],
"source": [
"print('régi format: %02X' % 11)\n",
"print('új format: {:02X}'.format(31))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"vajon mi a `'{:02X}'.format`???"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<function format>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'{:02X}'.format"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Egy függvény, de jó lenne, ha más lenne a neve - lenne egyáltalán neve!\n",
"\n",
"Függvényt értékül adjuk egy változónak - innentől a változó nevével már tudunk rá hivatkozni:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"tohex2_simplest = '{:02X}'.format\n",
"\n",
"assert '00' == tohex2_simplest(0)\n",
"assert '0F' == tohex2_simplest(15)\n",
"assert 'FF' == tohex2_simplest(255)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `tohex8(bytes)` általánosítása\n",
"\n",
"(ismét azok a stringek...)\n",
"\n",
"Működjön rövidebb byte listára is, az outputban a hiányzó input byte-ok helye legyen szóközzel kitöltve!"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "AssertionError",
"evalue": "Got '' instead of ' '",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-6-79ffe6365af5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[0mexpect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m' 00 00 00 00 00 00 00 00'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtohex8\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[0mexpect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m' 00 01 0F 00 00 00 80 FF'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtohex8\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m15\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m128\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m255\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m \u001b[0mexpect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m' '\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtohex8\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 13\u001b[0m \u001b[0mexpect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m' AA '\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtohex8\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m170\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<ipython-input-1-27a653c1143d>\u001b[0m in \u001b[0;36mexpect\u001b[1;34m(result, actual_result)\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mexpect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mactual_result\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m assert actual_result == result, (\n\u001b[1;32m----> 5\u001b[1;33m 'Got %r instead of %r' % (actual_result, result))\n\u001b[0m",
"\u001b[1;31mAssertionError\u001b[0m: Got '' instead of ' '"
]
}
],
"source": [
"def tohex8(bytes):\n",
" assert len(bytes) <= 8\n",
" # kiindulásnak jó ez, vagy a saját előző kódod (másold át)\n",
" # de valamit módosítani kell még rajta - javítsd ki!\n",
" output = ''\n",
" for byte in bytes:\n",
" output += ' ' + tohex2(byte)\n",
" return output\n",
"\n",
"expect(' 00 00 00 00 00 00 00 00', tohex8([0, 0, 0, 0, 0, 0, 0, 0]))\n",
"expect(' 00 01 0F 00 00 00 80 FF', tohex8([0, 1, 15, 0, 0, 0, 128, 255]))\n",
"expect(' ', tohex8([]))\n",
"expect(' AA ', tohex8([170]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `tohex16(bytes)`\n",
"\n",
"Formázzon 16 bytot a kapott listából (ha van annyi) két 8-as blokként úgy, hogy az első 8-as blokk után legyen még egy extra szóköz!\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def tohex16(bytes):\n",
" assert len(bytes) <= 16\n",
" # itt fel kéne használni a korábbiakat!\n",
"\n",
"\n",
"expect(\n",
" 8 * (1 + 2) + 1 + 8 * (1 + 2),\n",
" len(tohex16([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])))\n",
"expect(\n",
" ' 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00',\n",
" tohex16([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))\n",
"expect(\n",
" ' 00 01 0F 00 00 00 80 FF 00 00 00 00 00 00 00 ',\n",
" tohex16([0, 1, 15, 0, 0, 0, 128, 255, 0, 0, 0, 0, 0, 0, 0]))\n",
"expect(\n",
" ' ', tohex16([]))\n",
"expect(\n",
" ' AA ', tohex16([170]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `toascii(bytes)`\n",
"\n",
"Alakítsa a byte-okat stringgé, úgy, hogy\n",
"\n",
"- ha 0 <= `byte` < 32 az output '.'\n",
"- ha `byte` >= 127 az output '.'\n",
"- egyébként `chr(byte)`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def toascii(bytes):\n",
" ...\n",
"\n",
"\n",
"expect('', toascii([]))\n",
"expect('.', toascii([0]))\n",
"expect('..', toascii([0, 0]))\n",
"expect('..', toascii([127, 128]))\n",
"expect('hello', toascii([104, 101, 108, 108, 111]))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### toaddress(n)\n",
"\n",
"n formázása - jobbra igazított, fix hosszú stringgé"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def toaddress(n):\n",
" # 8 karakter:\n",
" return ' 123456'\n",
"\n",
"\n",
"expect(' 123456', toaddress(123456))\n",
"expect('12345678', toaddress(12345678))\n",
"expect(' 0', toaddress(0))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### dump_line(address, bytes)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def dump_line(address, bytes):\n",
" ...\n",
" \n",
"\n",
"expect(\n",
" ' 0: ',\n",
" dump_line(0, []))\n",
"expect(\n",
" ' 0: 01 .',\n",
" dump_line(0, [1]))\n",
"expect(\n",
" ' 128: 00 01 0F 00 00 00 80 FF 00 00 68 65 6C 6C 6F ..........hello',\n",
" dump_line(128, [0, 1, 15, 0, 0, 0, 128, 255, 0, 0, 104, 101, 108, 108, 111]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## file-ból olvasás"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Feladat1: internet keresés: hogyan lehet Python programban file-ból olvasni?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Feladat2: Egy tetszőleges file-ból olvassa be az első 16 byte-ot, és írja ki a **`dump_line()`**-nal!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## dict\n",
"\n",
"Adott nevek egy listája, egy név többször is szerepelhet benne.\n",
"\n",
"Rendeljen a nevekhez számokat, úgy hogy ha az elejéről nézzük a listát, akkor az `i`. számhoz tartozó név előtt pontosan `i` darab más név volt a listában (esetleg többször is!). Vagyis az eredmény legyen egy `dict`, amiben a kulcsok nevek, az értékek pedig a számok.\n",
"\n",
"Ez egy nehéz feladat, (legalább) kétféleképpen lehet nekilátni:\n",
"- ha már valameddig tudjuk a lista elejétől, hogy mi ez a hozzárendelés (\"részeredmény\"), akkor mi lesz a következő száma?\n",
"- az `i` szám pontosan a név indexe, ha a listából az ismétléseket töröljük (csak az első előfordulást tartjuk meg)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def make_name_id_map(names):\n",
" # your code here...\n",
" name_id_map = {}\n",
" return name_id_map\n",
"\n",
"\n",
"expect(\n",
" {'a': 0},\n",
" make_name_id_map(\n",
" ['a', 'a', 'a', 'a']))\n",
"\n",
"expect(\n",
" {'a': 0, 'c': 1},\n",
" make_name_id_map(\n",
" ['a', 'a', 'c', 'a']))\n",
"\n",
"expect(\n",
" {'a': 0, 'b': 1, 'c': 2},\n",
" make_name_id_map(\n",
" ['a', 'b', 'a', 'b', 'c', 'c']))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment