Commit c7f19a09 authored by JEAN-YVES SGRO's avatar JEAN-YVES SGRO
Browse files

Update...

Update 2023-Spring/2023-03-28-session-06/pseudocode_slides/pseudocode_beamer_slides.pdf, 2023-Spring/2023-03-28-session-06/pseudocode_slides/pseudocode_slides.docx, 2023-Spring/2023-03-28-session-06/pseudocode_slides/pseudocode_slides.pptx, 2023-Spring/2023-03-28-session-06/pseudocode_slides/pseudocode_document_slides.pdf, 2023-Spring/2023-03-28-session-06/pseudocode_slides/pseudocode_slides.Rmd, 2023-Spring/2023-03-28-session-06/pseudocode_slides/pseudocode_slidy.html, 2023-Spring/2023-03-28-session-06/pseudocode_slides/pseudocode_iosslides.html, 2023-Spring/2023-03-28-session-06/pseudocode_slides/pseudocode_html.html
parent 147765f1
Loading
Loading
Loading
Loading
+152 KiB

File added.

No diff preview for this file type.

+191 KiB

File added.

No diff preview for this file type.

+572 −0

File added.

Preview size limit exceeded, changes collapsed.

+3390 −0

File added.

Preview size limit exceeded, changes collapsed.

+185 −0
Original line number Diff line number Diff line
---
title: "Pseudocode"
author: "JYS"
date: "`r lubridate::today()`"
output:
  html_document: default
  beamer_presentation: default
  slidy_presentation:
    highlight: tango
  word_document: default
  ioslides_presentation:
    highlight: espresso
    fig_caption: no
    fig_height: 5
    fig_width: 7
    out_width: 70%
  powerpoint_presentation: default
  pdf_document: default
---

```{r setup, include=FALSE}
options(digits = 3)
knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE,
  cache = TRUE,
  out.width = "70%",
  fig.align = "center",
  fig.width = 6,
  fig.asp = 0.618,  # 1 / phi
  fig.show = "hold"
)

img_path <- "img"
```

```{r setupx, include=FALSE} 
## general set-up 
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning=FALSE, comment=NA) 
``` 



## Pseudocode

Summary of Python Pseudocode:

* Algorithmic representation to simplify the process of translating the code into an actual program.
* Syntax-free, easily translated to non-technical persons
* Useful to connect with people from other domains such as mathematicians, financial experts, biotechnologists, health care specialists, or any other specific domain experts.
* Pseudocode helps with application documentation and bug detection and fixing.
* Must be a close representation of the algorithmic logic: each line of the pseudocode should be proportionally translated to the actual code.

<i><font size = -1>
Source: https://www.educba.com/python-pseudocode/
</font></i>

## Five major Protocols in Python pseudocode

1. The pseudocode in python must be line by line so every statement involved must be represented as just one single line in pseudocode.
2. Just as in python code how indentations are used, these indentations must be preferred in python pseudocode too.
3. Ensure that each statement of the pseudocode is simple and easy to understand.
4. Ensure the first word of the pseudocode is always in Upper case letters.
5. Ensure to use periods and delimiters wherever needed.

## Example 01 {.smaller}

From https://www.educba.com/python-pseudocode/

```{python example01, eval=FALSE} 
# Retrieve the number to be reversed from the user into variable sample_number
sample_number = int(input("Number to be reversed: "))

# Initialize the temporary variable test_number as zero
test_number = 0

# Perform a while loop until the sample_number variable is greater than zero
while sample_number > 0:
    # Modulus the sample_number variable by 10 and store the remainder
    remainder = sample_number % 10
    
    # Multiply the temporary number of test_number by 10 and add the returned value to the remainder
    test_number = (test_number * 10) + remainder
    
    # Update the sample_number by dividing it by 10 (integer division)
    sample_number = sample_number // 10

# Print the generated test_number onto the console
print("Reversed number:", test_number)
``` 


## Original *vs* ChatGPT

The original version online from [Educba](https://www.educba.com/python-pseudocode/) only differed in the last line to print the result. However, the user would not see a difference.

```{python, eval=FALSE}
print("Value after reverse : {}".format(test_number))
```

ChatGPT version:

```{python, eval=FALSE}
print("Reversed number:", test_number)
```


## Example 02

Pseudo code from Chapter 1 of "Impractical Python projects: playful programming activities to make you smarter / Lee Vaughan."

```pseudocode
# Pseudocode for "Silly Name Generator"
Load a list of first names
Load a list of surnames
Choose a first name at random
Assign the name to a variable
Choose a surname at random
Assign the name to a variable
Print the names to the screen in order and in red font
             Ask the user to quit or play again
             If user plays again:
                 repeat
             If user quits:
end and exit
```

## Converted to Python by ChatGPT {.smaller}

```{python eval=FALSE}
import random
from termcolor import colored

# Define lists of first names and surnames
first_names = ['John', 'Mary', 'David', 'Emily', 'William']
surnames = ['Smith', 'Johnson', 'Brown', 'Taylor', 'Davis']

# Define a function to play the game
def play_game():
    # Choose a first name at random and assign it to a variable
    first_name = random.choice(first_names)

    # Choose a surname at random and assign it to a variable
    surname = random.choice(surnames)

    # Print the names to the screen in order and in red font
    print(colored(f"{first_name} {surname}", 'red'))

    # Ask the user to quit or play again
    choice = input("Do you want to play again? (y/n)")

    # If user plays again, call the play_game function again
    if choice == 'y':
        play_game()

# Call the play_game function to start the game
play_game()
```

## Pseudocode from Python code

It is also possible to rever the process, and write pseudo code from Python code.

A good example can be found on these resources:

* Site: https://ahclab.naist.jp/pseudogen/
* YouTube demo: https://youtu.be/cy_vKXuqT9g
* GitHub: https://github.com/delihiros/pseudogen

<Font Size = -1>
Paper: Pseudogen: A Tool to Automatically Generate Pseudo-code from Source Code 

  * http://www.phontron.com/paper/fudaba15asedemo.pdf
  * https://ahcweb01.naist.jp/papers/conference/2015/201511_ASE_Fudaba_1/201511_ASE_Fudaba_1.paper.pdf
</Font>

## Resources


| Title | Link | Archive |
| :--- | :---: | ---: |
| Introduction to Python pseudocode | https://www.educba.com/python-pseudocode/ | [Archived]("http://web.archive.org/web/20220000000000*/https://www.educba.com/python-pseudocode/") |
| 5 examples | https://sites.google.com/a/ismanila.org/oliverab_cp/python/pseudocode  | [Archived]("http://web.archive.org/web/20230325003645/https://sites.google.com/a/ismanila.org/oliverab_cp/python/pseudocode") |
|python / pseudocode interchange | http://www.pwnict.co.uk/webpages/python/convertPsuedo.html | [Archived]("http://web.archive.org/web/20211201000000*/http://pwnict.co.uk/webpages/python/convertPsuedo.html") |
Loading