String problem solving :: Python programming (2024)

Class Objectives

  • Practice problem solving in string data types.
  • Learn how to interpret real world problem into computer problem
  • More flexible usage of loops and conditionals
  • Dive deeper to string data structure
  • Process large and multi-dimensional string data in practice

Problems

  • Loop through strings
  • Character sorting
  • Type check
  • String reverse
  • Add suffix to string

Loop through strings

Requirements

Given a string consists of repeated characters. Write a function to process the string to return number of repeated characters in a dictionary.

  • Not allowed to use string.count(character) function
  • Your are encouraged to solve the problem with algorithms

Input:

string = "The more that you read, the more things you will know, the more that you learn, the more places you willl go."

Output:

{'T': 1, 'h': 7, 'e': 11, ' ': 21, 'm': 4, 'o': 10, 'r': 6, 't': 8, 'a': 5, 'y': 4, 'u': 4, 'd': 1, ',': 3, 'i': 3, 'n': 3, 'g': 2, 's': 2, 'w': 3, 'l': 7, 'k': 1, 'p': 1, 'c': 1, '.': 1}

Problem solving pattern

  • Step 1: Define a function with a parameter
  • Step 2: Define a dictionary
  • Step 3: Loop through the string
  • Step 4: Get keys of dictionary. keys = dict.keys() returns a list of keys
  • Step 5: Check if each character in the string exists in the keys.
  • Step 6: If it exists in the keys, increment it’s value by 1, if not assign a value 1.

Character sorting

Requirements

Given a string of random characters. Write a function to sort them in English alphabatical order.

  • Noe allowed to use Python sort functions
  • Solve the problem with algorithms

Problem solving pattern

Input:

string = 'dsajlvwjeoijvdskavljwioenvjskdlahj'

Output:

aaadddeehiijjjjjjkklllnoosssvvvvww

Hint

  • You may consider Python chr() and ord()

Type check

Requirements

Given string of random letters, digits and symbols. Write a function to return them in different lists.

  • You are encouraged to use regex

Input:

import re string = 'fjsdlkajlv463247836ewiFJKS&*$^&#$JFIEfjdksljVLWEJKjdkfjkd'

Output:

['f', 'j', 's', 'd', 'l', 'k', 'a', 'j', 'l', 'v', 'e', 'w', 'i', 'F', 'J', 'K', 'S', 'J', 'F', 'I', 'E', 'f', 'j', 'd', 'k', 's', 'l', 'j', 'V', 'L', 'W', 'E', 'J', 'K', 'j', 'd', 'k', 'f', 'j', 'k', 'd']['4', '6', '3', '2', '4', '7', '8', '3', '6']['&', '*', '$', '^', '&', '#', '$']

Problem solving pattern

  • Loop through the string by characters
  • Check each character if it is the type of letters, digits or symbols.
  • Return each list of result

String reverse

Requirements

Write a function to reverse a given string.

  • Your are encouraged to use algorithms
  • Not allowed to Python embedded reverse functions

Input:

string = "Children must be raught how to think, not what to think"

Output:

"kniht ot tahw ton ,kniht ot woh thguar eb tsum nerdlihC"

Problem solving pattern

  • Step 1: Define a function with a parameter
  • Step 2: Define a empty string variable
  • Step 3: Loop through the string with for loop
  • Step 4: Get the characters from the end until the beginning.
  • Step 5: Add each character into the result string

Add suffix to string

Requirements

Given a string consists of different words end with ing. Write a function to append ly to words end with ing, and ing to those are not end with ing, ignore those already are end with ly.

  • Not allowed to use Python endswith()
  • You are encouraged to use algorithms

Input:

string = "He is fully absessed with eating. Eating and eating, don't stop forever!"

Output:

"Heing ising fully absesseding withing eatingly. Eatingly anding eatingly, don'ting stoping forever!ing"

Problem solving pattern

  • Step 1: Write a function with a parameter
  • Step 2: Loop through the string to get each word
  • Step 3: Check if each word ends with ing or ly by splitting the word
  • Step 4: Add each word appropriate suffix and return the result
String problem solving :: Python programming (2024)
Top Articles
Debit Card Alerts Questions – Wells Fargo
[object Object]
Coverage of the introduction of the Water (Special Measures) Bill
Unitedhealthcare Hwp
Soap2Day Autoplay
Fire Rescue 1 Login
Edgar And Herschel Trivia Questions
Chastity Brainwash
R Tiktoksweets
Evangeline Downs Racetrack Entries
ocala cars & trucks - by owner - craigslist
Conscious Cloud Dispensary Photos
Theresa Alone Gofundme
Swedestats
Troy Bilt Mower Carburetor Diagram
Unterwegs im autonomen Freightliner Cascadia: Finger weg, jetzt fahre ich!
R Personalfinance
Hennens Chattanooga Dress Code
Drago Funeral Home & Cremation Services Obituaries
Pecos Valley Sunland Park Menu
Aerocareusa Hmebillpay Com
Magic Seaweed Daytona
Play It Again Sports Norman Photos
Vivaciousveteran
Colonial Executive Park - CRE Consultants
Helpers Needed At Once Bug Fables
Maths Open Ref
Airg Com Chat
Rust Belt Revival Auctions
Reborn Rich Ep 12 Eng Sub
Eleceed Mangaowl
Usf Football Wiki
Bimmerpost version for Porsche forum?
Studio 22 Nashville Review
Mars Petcare 2037 American Italian Way Columbia Sc
Insideaveritt/Myportal
Craigslist en Santa Cruz, California: Tu Guía Definitiva para Comprar, Vender e Intercambiar - First Republic Craigslist
Weekly Math Review Q2 7 Answer Key
Kaamel Hasaun Wikipedia
Workday Latech Edu
Okta Login Nordstrom
Food and Water Safety During Power Outages and Floods
All Buttons In Blox Fruits
SF bay area cars & trucks "chevrolet 50" - craigslist
Blog Pch
Sam's Club Fountain Valley Gas Prices
Ark Silica Pearls Gfi
King Fields Mortuary
Arre St Wv Srj
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 6797

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.