Advertisement
rric

jandlize_a_poem

Nov 6th, 2023 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.02 KB | None | 0 0
  1. # Jandlize a poem, and learn to apply some Python strings functions [Mu: Python 3]
  2. # Copyright 2021--2023 Roland Richter
  3.  
  4. # The code below has several bugs that cause all assert()
  5. # statements to fail. Your task is to FIX these errors ...
  6. # but you may edit the code ONLY in between the ↓ / ↑ lines.
  7. # Let's start with a simple example:
  8.  
  9. # FIX the spelling so that salutation is "Hello, world!"
  10. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  11. salutation = "Hell0, wolrd!"
  12. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  13.  
  14. assert salutation == "Hello, world!", "'" + salutation + "' ≠ 'Hello, world!'"
  15.  
  16. # If you fixed the first error, open a handbook such as
  17. # https://www.w3schools.com/python/python_strings.asp
  18. # and continue to fix bugs, until the program runs to its end.
  19. # Remember that you MUST NOT edit code outside the ↓ / ↑ lines!
  20.  
  21. # FIX a horizontal rule should be 6 times '-----'
  22. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  23. horizontal_rule = 42 * "-"
  24. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  25.  
  26. assert horizontal_rule == 6 * "-----", "horizontal_rule ≠ 6 * '-----'"
  27.  
  28. title = " lichtung von Ernst Jandl  "
  29.  
  30. # FIX remove leading and trailing spaces from the title
  31. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  32. title = title
  33. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  34.  
  35. assert title == "lichtung von Ernst Jandl", (
  36.     "'" + title + "' ≠ 'lichtung von Ernst Jandl'"
  37. )
  38.  
  39. def dont_compute_lenght(s):
  40.     return 0
  41.  
  42. # FIX use a build-in function to compute the length of the title
  43. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  44. title_length = dont_compute_lenght(title)
  45. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  46.  
  47. assert title_length == 24, str(title_length) + " ≠ 24"
  48.  
  49. # FIX extract the name "Ernst Jandl" from the title string
  50. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  51. name = title[5:]
  52. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  53.  
  54. assert name == "Ernst Jandl", "'" + name + "' ≠ 'Ernst Jandl'"
  55.  
  56. line_one = "MancHe MeiNEN"
  57.  
  58. # FIX convert all upper case letters to lower case, and add
  59. #     two spaces at the beginning of line_one
  60. # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  61. line_one = line_one
  62. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  63.  
  64. assert line_one == "  manche meinen", "'" + line_one + "' ≠ '  manche meinen'"
  65.  
  66. # FIX modify the function so that it does what it states in the docstring
  67. # ↓---------↓---------↓---------↓---------↓---------↓---------↓---------↓
  68. def jandlize(text):
  69.     """Return the text with each letter converted to lower case,
  70.        and letters 'l' and 'r' swapped. For example, the sentence
  71.        "It was a bright cold day in April" becomes
  72.        "it was a blight cord day in aplir"
  73.  
  74.    Arguments:
  75.    text -- the text to be jandlized.
  76.    """
  77.     result = ""
  78.     for letter in text:
  79.         if letter == "r":
  80.             result += "l"
  81.         else:
  82.             result += letter
  83.     return result
  84. # ↑---------↑---------↑---------↑---------↑---------↑---------↑---------↑
  85.  
  86. line_two = jandlize("  Rechts und links")
  87. line_three = jandlize("  kann man nicht verwechseln")
  88. line_four = jandlize("  Welch ein Irrtum")
  89.  
  90. assert line_two == "  lechts und rinks", "'" + line_two + "' ≠ '  lechts und rinks'"
  91. assert line_three == "  kann man nicht velwechsern", (
  92.     "'" + line_three + "' ≠ '  kann man nicht velwechsern'"
  93. )
  94. assert line_four == "  werch ein illtum", "'" + line_four + "' ≠ '  werch ein illtum'"
  95.  
  96.  
  97. print("+---------+---------+---------+---------+---------+---------+---------+")
  98. input("FIXED You did it! Press any key to see the poem, plus some information ...")
  99.  
  100. print(horizontal_rule)
  101. print(title)
  102. print(line_one)
  103. print(line_two)
  104. print(line_three)
  105. print(line_four)
  106. print(horizontal_rule)
  107.  
  108. print(
  109.     """The poem 'lichtung' by Ernst Jandl was published in his
  110. first volume of poems, 'Laut and Luise', in 1966.
  111. Original version: http://lechts.at/lichtung/
  112. De-jandlized poem: http://rinks.at/richtung/"""
  113. )
  114.  
  115. # ----------------------------------------------------------------------
  116. # This program is free software: you can redistribute it and/or modify
  117. # it under the terms of the GNU General Public License as published by
  118. # the Free Software Foundation, either version 3 of the License, or
  119. # (at your option) any later version.
  120. #
  121. # This program is distributed in the hope that it will be useful,
  122. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  123. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  124. # GNU General Public License for more details.
  125. #
  126. # You should have received a copy of the GNU General Public License
  127. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement