Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* levelezősök
  + Adator. prog.
  + feladatsor
  + quick link

teaching assets


Félévek

* 2024/25/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

20130919b

a-z; with reversed source code: z-a

Write a Python script that prints the lower case letters of the English alphabet from a to z.

Now reverse the source code of the script. The reversed source should also give a valid Python script, and if you run it, you should get the lower case letters of the English alphabet in a reversed way from z to a.

Tip: pass your Python script to the Python interpreter, as seen below in the running example.

To reverse a file, use this program (reverse.py):

#!/usr/bin/env python3

import sys

def main():
    with open(sys.argv[1]) as f:
        print(f.read()[::-1])

##############################################################

if __name__ == "__main__":
    if len(sys.argv) == 1:
        print("One parameter is required.")
        sys.exit(1)
    # else
    main()

Running example:

$ ls -al
total 20
drwxrwxr-x 2 jabba jabba 4096 Sep 19 13:31 .
drwxrwxr-x 3 jabba jabba 4096 Sep 19 13:26 ..
-rwx------ 1 jabba jabba  124 Sep 19 13:31 a-z.py
-rwx------ 1 jabba jabba  313 Sep 19 13:22 reverse.py
$ python ./a-z.py 
abcdefghijklmnopqrstuvwxyz
$ ./reverse.py a-z.py >back.py 
$ ls -al
total 20
drwxrwxr-x 2 jabba jabba 4096 Sep 19 13:31 .
drwxrwxr-x 3 jabba jabba 4096 Sep 19 13:26 ..
-rwx------ 1 jabba jabba  124 Sep 19 13:31 a-z.py
-rw-rw-r-- 1 jabba jabba  125 Sep 19 13:31 back.py
-rwx------ 1 jabba jabba  313 Sep 19 13:22 reverse.py
$ python back.py 
zyxwvutsrqponmlkjihgfedcba
$

The file "back.py" may be 1 byte longer. It is due to an extra newline character.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2017 August 04, 11:36