Introduction
🚀 Let’s dive into the world of AI together, shall we?
AI like ChatGPT can be the big brother assistant you need. Not a “big brother” that spies on you, ,more like the one that can explain you things in a different way than your teacher or your book.
Here are examples of things you can do with AI :-)
Music Intro
Translate from one language to another
I have added Google Translate for an easy translation of this site into other languages. It can be found in the upper right corner:
ChatGPT 4o is a much better translator than Google and if something doesn’t make sense, then please use it instead. Just mark what you want translated and use the following prompt:
Can you please translate the following into danish?
--
<insert your text here>
--
Can you please translate the following into danish?
--
Introduction
Imagine you’re playing Minecraft, and you need to move as close to the edge as possible, to set the next block, without falling off the platform. Boundary-Value Analysis (BVA) in software testing is like that, where we find out where the edge of not-falling and falling is.
Example:
Scenario Outline: Withdraw boundary-adjacent values
Given the user has an active bank account
When the user tries to withdraw <amount> dollars
Then the withdrawal should be <result>
Examples:
| amount | result | comment
| 499 | successful | # Just below maximum boundary
| 500 | successful | # Maximum boundary
| 501 | rejected | # Just above maximum boundary
📝 AI promptSure, here is the translation into Danish:
---
Introduktion
Forestil dig, at du spiller Minecraft, og du skal bevæge dig så tæt på kanten som muligt for at placere den næste blok uden at falde af platformen. Grænseværdianalyse (BVA) i softwaretestning er som det, hvor vi finder ud af, hvor grænsen mellem ikke-at-falde og at-falde er.
Eksempel:
Scenarieoversigt: Udtage grænse-tilstødende værdier
Givet at brugeren har en aktiv bankkonto
Når brugeren forsøger at hæve <beløb> dollars
Så skal hævningen være <resultat>
Eksempler:
| beløb | resultat | kommentar
| 499 | succesfuld | # Lige under maksimal grænse
| 500 | succesfuld | # Maksimal grænse
| 501 | afvist | # Lige over maksimal grænse
🤖 AI responseTranslate to alternative explanation
I don’t explain everything in an easy way.
AI can help explain the topic in a different way. Maybe for a 15 year?
Maybe in the context of banking? or in the context of project management?
It can be important to tell the AI to use max. 500 characters, to make the explanation short.
Prompt example 1, for a 15 year old:
What is the following text about? Explain it like I am 15 years old. Max. 500 characters.
--
Length of a string
We can also measure the length of a string, which can be important if we want a password to have a minimum length.
Python
# when we have two passwords
password1 = "12345678"
password2 = "1234567"
# then password1 is long enough (this will pass)
assert len(password1) >=8
# and password2 is not long enough (this will fail)
assert len(password2) >=8
--
📝 AI promptThis text is about checking how long a string (like a password) is in Python.
It explains how to make sure a password meets a minimum length requirement, which is important for security.
The example shows two passwords: one with 8 characters and one with 7 characters.
The first password is long enough, but the second one isn't.
The assert statements check if each password is at least 8 characters long, passing for the first and failing for the second.
🤖 AI responsePrompt example 2, for a baker:
What is the following text about? Explain it like I am a baker and have never seen code before. Max. 500 characters.
--
Length of a string
We can also measure the length of a string, which can be important if we want a password to have a minimum length.
Python
# when we have two passwords
password1 = "12345678"
password2 = "1234567"
# then password1 is long enough (this will pass)
assert len(password1) >=8
# and password2 is not long enough (this will fail)
assert len(password2) >=8
--
📝 AI promptImagine you have two loaves of bread. To sell them, each loaf must weigh at least 8 ounces.
Loaf 1 weighs 8 ounces (good to sell).
Loaf 2 weighs 7 ounces (not good to sell).
In baking terms, the code checks if each "loaf" (or password) is heavy enough (or long enough).
If Loaf 1 passes the check, it's fine.
If Loaf 2 fails, it's too light (or short).
This helps ensure each "loaf" (or password) meets a standard weight (or length) for quality.
🤖 AI responseTranslate from one programming language to another
AI is quite good to translate between programming languages.
It can take Python and translate it into functioning JavaScript, Groovy or C#.
It’s pretty sweet.
The future of code is therefore in the conceptual: “to learn to express one self more precise” and not to “connecting logic into hardware”, which the machines will dominate.
Can you please translate the following Python script into JavaScript:
--
# Given
number1 = 1
number2 = 2
# When
result = number1 + number2
# Then
print(result)
--
📝 AI promptSure, here is the translated Python script into JavaScript:
// Given
let number1 = 1;
let number2 = 2;
// When
let result = number1 + number2;
// Then
console.log(result);
🤖 AI responseIt can also be used to translate from one framework to another!
Translate human parts of code from one human language to another
We can also translate code examples, where we only translate: comments, variable names, and text strings.
It is important not to translate the code itself, because Python, JavaScript, Groovy, and C# will not understand it. Print needs to remain print, assert needs to remain assert, etc.!
Can you please translate the following into danish?
(only comments, variable names and string content, not code)
--
# given we have two texts
text1 = "hello"
space = " "
text2 = "world"
# when we add them together
result = text1 + space + text2
# then they put together
print(result)
assert result == "hello world"
📝 AI prompt# givet at vi har to tekster
tekst1 = "hej"
mellemrum = " "
tekst2 = "verden"
# når vi lægger dem sammen
resultat = tekst1 + mellemrum + tekst2
# så bliver de sat sammen
print(resultat)
assert resultat == "hej verden"
🤖 AI responseExplain code with Given, when, and then
AI can also be used to explain code in the Gherkin language (given, when, and then).
When we have the following code:
text1 = "hello"
space = " "
text2 = "world"
result = text1 + +space + text2
print(result)
💻 Codethen we can translate it into Gherkin language, with the following prompt:
I have the following python code, and I would like you to add Given, When, Then comments into it, so they explain the code (for a 15 year old):
--
text1 = "hello"
space = " "
text2 = "world"
result = text1 + +space + text2
print(result)
--
📝 AI promptSure, here are the Given, When, Then comments added to the code with explanations suitable for a 15-year-old:
# Given: We have three pieces of text: 'hello', a space ' ', and 'world'
text1 = "hello"
space = " "
text2 = "world"
# When: We try to join these pieces of text together with a space in between
result = text1 + space + text2
# Then: We want to see the combined text, which should be 'hello world'
print(result)
This way, the comments are clear and easy to understand for a 15-year-old.
🤖 AI responseThis was a simple translation, but it can also be used for more cryptic code:
number = 10
result = "{:05d}".format(10)
print(result)
💻 CodeI have the following python code, and I would like you to add Given, When, Then comments into it, so they explain the code (for a 15 year old):
--
number = 10
result = "{:05d}".format(10)
print(result)
--
📝 AI prompt# Given: We have a number 10
number = 10
# When: We format the number to have 5 digits, padding with zeros if needed
result = "{:05d}".format(number)
# Then: We want to see the formatted number, which should be '00010'
print(result)
🤖 AI responseThe AI still have trouble with larger pieces of code, it could be a sign though, that this large piece of code should be split into smaller methods, each doing their thing!
Translate given, when, then to code
AI can also be used to explain code in the Gherkin language (given, when, and then).
When we use following prompt
I have the following comments in gherkin, and would like you to translate it to Python code:
--
Given a variable called number has value <value>
When <before> zeroes are added before the number
And <after> zeroes are added after the number
Then the result must be printed, which must be <expected>
Examples
| value | before | after | expected |
| 7 | 3 | 2 | 000700 |
| 12 | 2 | 3 | 0012000 |
--
📝 AI promptdef add_zeroes(value, before, after):
# Convert value to string
value_str = str(value)
# Add 'before' number of zeroes before the value
value_str = '0' * before + value_str
# Add 'after' number of zeroes after the value
value_str = value_str + '0' * after
return value_str
# Test cases
examples = [
{'value': 7, 'before': 3, 'after': 2, 'expected': '000700'},
{'value': 12, 'before': 2, 'after': 3, 'expected': '0012000'}
]
# Run test cases
for example in examples:
value = example['value']
before = example['before']
after = example['after']
expected = example['expected']
result = add_zeroes(value, before, after)
assert result == expected, f"Test failed: expected {expected} but got {result}"
print(f"Test passed: {value} with {before} zeroes before and {after} zeroes after results in {result}")
🤖 AI responseThis not only creates the method for us, but also creates the required test cases that reports a good output!
And when the code run:
(try it your self in our web editors): 💻 Python / 💻 JavaScript / 💻 Groovy / 💻 C#
Python3 2024-06-03 on Linux x86_64)
----
Test passed: 7 with 3 zeroes before and 2 zeroes after results in 000700
Test passed: 12 with 2 zeroes before and 3 zeroes after results in 0012000
💻 Terminal resultYou can always add another test case, just to be sure that the code actually works:
def add_zeroes(value, before, after):
# Convert value to string
value_str = str(value)
# Add 'before' number of zeroes before the value
value_str = '0' * before + value_str
# Add 'after' number of zeroes after the value
value_str = value_str + '0' * after
return value_str
# Test cases
examples = [
{'value': 7, 'before': 3, 'after': 2, 'expected': '000700'},
{'value': 12, 'before': 2, 'after': 3, 'expected': '0012000'},
{'value': 123, 'before': 4, 'after': 1, 'expected': '00001230'}
]
# Run test cases
for example in examples:
value = example['value']
before = example['before']
after = example['after']
expected = example['expected']
result = add_zeroes(value, before, after)
assert result == expected, f"Test failed: expected {expected} but got {result}"
print(f"Test passed: {value} with {before} zeroes before and {after} zeroes after results in {result}")
🤖 AI responsePython3 2024-06-03 on Linux x86_64)
----
Test passed: 7 with 3 zeroes before and 2 zeroes after results in 000700
Test passed: 12 with 2 zeroes before and 3 zeroes after results in 0012000
Test passed: 123 with 4 zeroes before and 1 zeroes after results in 00001230
💻 Terminal resultHow cool is that?
And you can get good code examples for:
- How to read Excel data
- Making a post request with a security token (yea, no need for PostMan :D)
- Uploading a File to an FTP Server
- Parse objects into JSON
- Using Kafka
- Reading from a MongoDB
- Sending an email notifications
- Etc.
Debugging your code with AI
AI as ChatGPT 4o can help us debug simple things or give us ideas for where a bug might be.
When we have the following code:
# Given
const number1 = 1
const number2 = 2
# When
const result = number1 + number2
# Then
print(result)
💻 Codeand try it in the terminal or a web editor: 💻 Python / 💻 JavaScript / 💻 Groovy / 💻 C#
then it will fail with the following result:
Python3 2024-06-02 on Linux x86_64)
----
err
File "apps/console-python/", line 2
const number1 = 1
^^^^^^^
SyntaxError: invalid syntax
💻 Terminal resultUse the following AI prompt to get help:
I have the following python code:
# Given
const number1 = 1
const number2 = 2
# When
const result = number1 + number2
# Then
print(result)
--
That gives me the following error:
--
err
File "apps/console-python/", line 2
const number1 = 1
^^^^^^^
SyntaxError: invalid syntax
--
Can you help me understand what is wrong?
📝 AI promptThe error you are encountering is due to the use of const, which is not a keyword in Python. In Python, variables are not declared with const like in JavaScript. Instead, you simply assign values to variables directly.
Here is the corrected Python code:
# Given
number1 = 1
number2 = 2
# When
result = number1 + number2
# Then
print(result)
Explanation:
* Variable Declaration: In Python, you just use the variable name directly without const.
* Syntax: The syntax in Python for variable assignment is straightforward, as shown above.
With these changes, your code should work correctly and print the result: 3
🤖 AI responseThis is an excellent answer and it works very well for simple problems.
In a few years it will probably be able to help with greater problems :-)
Conclusion
- Translate from one language to another: We learned how to use Google Translate for basic translations and ChatGPT for more accurate translations, particularly when translating into Danish.
- Translate to alternative explanation: We discovered how AI can provide alternative explanations for code, tailored to different audiences, such as a 15-year-old or a baker, making complex concepts more accessible.
- Translate from one programming language to another: We explored how to translate the human-readable parts of code, such as comments and variable names, between different programming languages while maintaining the original code syntax.
- Translate human parts of code from one human language to another: We understood the importance of translating only the human-readable parts of code, ensuring clarity in multilingual coding environments, with a specific example of Danish translation.
- Explain code with Given, when, and then: We learned how to explain code using the Gherkin language structure (Given, When, Then) to simplify understanding for beginners, demonstrated with straightforward Python examples.
- Translate given, when, then to code: We practiced converting Gherkin language descriptions back into Python code, including the creation of test cases to ensure the functionality of the code.
- Debugging your code with AI: We explored how AI, like ChatGPT, can assist in debugging code by providing useful feedback and corrections, illustrated with an example of correcting JavaScript syntax errors in Python code.
By integrating these skills, we have enhanced our ability to translate, explain, and debug code across languages and contexts, leveraging AI to facilitate better understanding and more efficient problem-solving in programming.
And the best part is: We learn to communicate more precise with the machines, when they will build our future hospitals, schools, and other important institutions!