Skip to content

Commit b9789a5

Browse files
Merge branch 'codewithdhruba01:master' into feature/max-subarray-sum
2 parents 8f525cf + 0ba8a7e commit b9789a5

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

01_Introduction/README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
# Python Introduction
2-
## What is a program?
3-
A ***program*** is a sequence of instructions that specifies how to perform a computation. The
4-
computation might be something mathematical, such as solving a system of equations or
5-
finding the roots of a polynomial, but it can also be a symbolic computation, such as searching and replacing text in a document or something graphical, like processing an image or
6-
playing a video.
7-
8-
- ***Input:*** Get data from the keyboard, a file, the network, or some other device.
9-
- ***Output:*** Display data on the screen, save it in a file, send it over the network, etc.
10-
- ***Math:*** Perform basic mathematical operations like addition and multiplication.
11-
- ***Conditional execution:*** Check for certain conditions and run the appropriate code.
1+
## What is a python?
2+
**Python** is a **high-level, interprited, interactive,** and **Obeject oriented** scripting language.Its readability and ease of use, it was created by **Guido van Rossum,** and **released in 1991.**
123

134
---
145
### It is used for:
@@ -36,6 +27,7 @@ playing a video.
3627
- 🔹 The latest stable version is **Python 3** (recommended for use).
3728
- 🔹 Python 2 is still used but only receives security updates.
3829
- 🔹 Python code can be written in any **text editor** or in an **IDE** like:
30+
- **VSCode**
3931
- **Thonny**
4032
- **PyCharm**
4133
- **NetBeans**

longest_word.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import unittest
2+
import string
3+
import sys
4+
5+
def find_longest_word(sentence):
6+
# Remove punctuation
7+
cleaned_sentence = sentence.translate(str.maketrans('', '', string.punctuation))
8+
words = cleaned_sentence.split()
9+
longest = ""
10+
11+
for word in words:
12+
if len(word) > len(longest):
13+
longest = word
14+
15+
return longest
16+
17+
class TestFindLongestWord(unittest.TestCase):
18+
19+
def test_regular_sentence(self):
20+
self.assertEqual(find_longest_word("Random sentences can also spur creativity"), "creativity")
21+
22+
def test_with_punctuation(self):
23+
self.assertEqual(find_longest_word("The quick, brown fox jumped over the lazy dog."), "jumped")
24+
25+
def test_tie_same_length(self):
26+
self.assertEqual(find_longest_word("Cat bat rat mat"), "Cat")
27+
28+
def test_single_word(self):
29+
self.assertEqual(find_longest_word("Supercalifragilisticexpialidocious!"), "Supercalifragilisticexpialidocious")
30+
31+
def test_with_numbers_and_punctuation(self):
32+
self.assertEqual(find_longest_word("Python 3.10 is awesome, right?"), "awesome")
33+
34+
def test_empty_input(self):
35+
self.assertEqual(find_longest_word(""), "")
36+
37+
def test_only_punctuation(self):
38+
self.assertEqual(find_longest_word("!!! ,,, ???"), "")
39+
40+
def interactive_mode():
41+
"""Provides interactive input for the user to test the function."""
42+
print("Enter sentences to find the longest word, or type 'q' to quit.")
43+
while True:
44+
sentence = input("Enter a sentence: ")
45+
if sentence.lower() == 'q':
46+
break
47+
print("Longest word:", find_longest_word(sentence))
48+
print("Goodbye!")
49+
50+
51+
if __name__ == '__main__':
52+
# If "test" is provided as an argument, run unit tests.
53+
if len(sys.argv) > 1 and sys.argv[1] == "test":
54+
# Remove the "test" argument so unittest doesn't process it.
55+
sys.argv.pop(1)
56+
unittest.main()
57+
else:
58+
interactive_mode()

0 commit comments

Comments
 (0)