UNCLASSIFIED

Skip to content
Snippets Groups Projects
Commit 79293cc2 authored by kmc-home's avatar kmc-home
Browse files

add string challenges

parent 63ac61a4
Branches
No related merge requests found
Pipeline #120191 passed with stage
in 11 seconds
{
"python.analysis.typeCheckingMode": "basic"
}
\ No newline at end of file
# -- ex_1 -- #
# Write a program that prompts a user to enter a whole number.
# - Determine if the string contains all numeric characters.
# - An `if` statement is not required, string methods will suffice.
def ex_1():
pass
# -- ex_2 -- #
# Write a program that prompts a user to enter a floating point number.
# - Determine if the string can be safely converted to a `float`
# - An `if` statement is not required, string methods will suffice.
def ex_2():
pass
# -- ex_3 -- #
# Write a program that prompts a user to enter a string of text and another
# prompt for a character or sequence of characters to search for in the text.
# * The program should print:
# ** The number of times the character or sequence of characters to search
# for occurs in the original string of text.
# ** The first index position of the character or sequence of characters to search for.
# *** The program should not crash if the character(s) to search for do not
# occur in the original text.
def ex_3():
pass
# -- ex_4 -- #
# Write a program that prompts a user to enter a string of text and another
# prompt for a character or sequence of characters to search for in the text.
# * Make the program perform a case insensitive search.
# * The program should print:
# ** The number of times the character or sequence of characters to search
# for occurs in the original string of text.
# ** The index position of the character or sequence of characters to search for.
# *** The program should not crash if the character(s) to search for do not
# occur in the original text.
def ex_4():
pass
# -- ex_5 -- #
# Write a program that prompts a user to enter a url.
# * The program should extract the host name and the domain into separate variables.
# ** Print the host name and domain.
def ex_5():
pass
# -- ex_6 -- #
# Write a program that produces the following output.
# [listing]
# ----
# Casey at the Bat by Ernest Thayer
# And somewhere men are laughing,
# and somewhere children shout,
# But there is no joy in Mudville
# mighty Casey has struck out!
# ----
def ex_6():
pass
# -- ex_7 -- #
# Write a program that prints a royal straight flush of spades.
def ex_7():
pass
# -- ex_8 -- #
# Write a program that requests a sentence, then print out the first and last
# words of the sentence, and finally, the number of words in the sentence.
# * Any trailing punctuation, such as a period, should not be included in the last word.
def ex_8():
pass
# -- ex_9 -- #
# Write a program that takes a number as input, and prints out the ASCII
# character associated with that value.
def ex_9():
pass
if __name__ == "__main__":
ex_1()
# ex_2()
# ex_3()
# ex_4()
# ex_5()
# ex_6()
# ex_7()
# ex_8()
# ex_9()
\ No newline at end of file
def ex_1():
user_input = input("Enter a whole number: ")
print(user_input.isnumeric())
def ex_2():
user_input = input("Enter a floating point number: ")
print(user_input.replace('.', '', 1).isdigit())
# Alternative, handles negative numbers, but harder to read:
# print(user_input.lstrip('-').replace('.', '', 1).isdigit())
def ex_3():
text = input("Enter a string of text: ")
search_term = input("Enter a character or sequence of characters to search for: ")
count = text.count(search_term)
print(f"The search term occurs {count} times.")
index = text.find(search_term) # Use find() instead of index()
if index != -1:
print(f"The first index position of the search term is: {index}")
else:
print("The search term was not found in the text.")
def ex_4():
text = input("Enter a string of text: ").lower()
search_term = input("Enter a character or sequence of characters to search for: ").lower()
count = text.count(search_term)
print(f"The search term occurs {count} times.")
index = text.find(search_term) # Use find() instead of index()
if index != -1:
print(f"The first index position of the search term is: {index}")
else:
print("The search term was not found in the text.")
def ex_5():
url = input("Enter a URL: ")
# Remove protocol (http:// or https://)
url = url.replace("http://", "").replace("https://", "")
# Remove path and query string
parts = url.split('/', 1)
host_and_domain = parts[0]
# Split into hostname and domain
parts = host_and_domain.split('.', 1)
# Handle cases with no subdomain (e.g., example.com)
if len(parts) == 2:
host_name = parts[0]
domain = parts[1]
else: #handles edge cases
host_name = ""
domain = parts[0]
print(f"Host name: {host_name}")
print(f"Domain: {domain}")
def ex_6():
print("Casey at the Bat by Ernest Thayer\n")
print(" And somewhere men are laughing,")
print(" and somewhere children shout,")
print(" But there is no joy in Mudville")
print(" mighty Casey has struck out!")
def ex_7():
print("10♠ J♠ Q♠ K♠ A♠")
def ex_8():
sentence = input("Enter a sentence: ")
words = sentence.split()
if words: # Check if the list is not empty
first_word = words[0]
last_word = words[-1].rstrip('.,!?"\'') # Remove trailing punctuation
num_words = len(words)
print(f"First word: {first_word}")
print(f"Last word: {last_word}")
print(f"Number of words: {num_words}")
else:
print("No words entered.")
def ex_9():
num_str = input("Enter a number: ")
if num_str.isdigit():
num = int(num_str)
if 0 <= num <= 127: # Check for valid ASCII range (0-127 for basic ASCII)
print(chr(num))
else:
print("Number out of valid ASCII range. Please choose between 0-127")
else:
print("Invalid input. Please enter a whole number.")
\ No newline at end of file
import dis
def hello():
print("Hello, World!")
dis.dis(hello)
print("hello world")
x = 12
import os
......@@ -5,6 +5,7 @@ description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"pylint>=3.3.4",
"python-openstackclient>=7.2.1",
]
......
......@@ -294,6 +294,7 @@ name = "delta-students-cybbh"
version = "0.1.0"
source = { virtual = "." }
dependencies = [
{ name = "pylint" },
{ name = "python-openstackclient" },
]
......@@ -308,7 +309,10 @@ dev = [
]
[package.metadata]
requires-dist = [{ name = "python-openstackclient", specifier = ">=7.2.1" }]
requires-dist = [
{ name = "pylint", specifier = ">=3.3.4" },
{ name = "python-openstackclient", specifier = ">=7.2.1" },
]
[package.metadata.requires-dev]
dev = [
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment