
Effective Strategies for Solving Programming Problems
Discover how to solve programming problems efficiently by breaking them down into manageable steps, understanding key concepts like string manipulation, and using Python to implement solutions. Explore topics such as string basics, slicing, comparison, methods, and more to enhance your problem-solving skills.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
CSE 231 LAB 3
TOPICS TO COVER How to solve a problem Strings Basics String Slicing String Comparison String Methods Patterns to Build Strings String Formatting
HOW TO SOLVE A PROBLEM Do not do everything at once! Solve the problem in your own words and write it down (written steps, flowcharts, or maybe pseudo code) Once you have a solution, translate it to Python and adjust as needed This significantly reduces the complexity of getting a solution.
HOW TO SOLVE A PROBLEM Break it down. Any problem is the sum of many smaller and more manageable pieces. When reading the PDF/ directions, identify key words and phrases (I.E. What happens, what it happens to, what the result is, and how it happens). Ex. Create a program that, given 2 integers [Start with this], returns [indicates the result is next] the greatest common divisor[result] without using math module [restriction] Check the FAQ for the projects on Piazza.
STRINGS BASICS REVIEW >> input_str = Hello World H e l 0 1 l 3 o 4 W o r 6 7 l d 9 10 2 5 8 >> print(input_str)? Hello World <class 'str'> >> print(type(input_string))? >> print(len(input_str))? 11 >> print(input_str[1])? e
STRINGS BASICS REVIEW >> input_str = Hello World H 0 e 1 l l o W o r l d 2 3 4 5 6 7 8 9 10 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 >> print(input_str[-1])? d >> print(input_str[-5])? W >> print(input_str[-10])? e
SLICING Start end input_str[ : ] Does not include Includes
>> input_str = Hello World H 0 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 e 1 l l o 3 W o 6 r 8 l d 2 4 5 7 9 10 Worl World >> print(input_str[6:10])? >> print(input_str[6:11])? World Hello >> print(input_str[6:])? >> print(input_str[:5])? Hello >> print(input_str[0:5])? lo Wor >> print(input_str[3:-2])?
EXTENDED SLICING >> input_str = Hello World H 0 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 e 1 l l o 3 W o 6 r 8 l d 2 4 5 7 9 10 >> print(input_str[::2])? HloWrd >> print(input_str[::-2])? drWolH >> print(input_str[1::2])? el ol
STRING COMPARISON a < b hi > hello ASCII table
STRING METHODS Methods are like functions but specific to certain classes .isupper() #checks if entire string is upper case .isdigit() #checks if entire string is a digit (number) .isalpha() #checks if entire string is all letters(word) .lower() #converts string to all lower case .find(ch) #returns the first index where ch is found, -1 if not .rfind(ch) #returns the last index where ch is found, or -1 if not .replace(old, new) #returns a new string where all occurrences of old substring is replaced with new substring.
PATTERNS TO BUILD STRING Ints: x = 0 x += 1 Strings: s = s += ch # initialize at zero # increment the counter # initialize with empty string # concatenate characters
ENUMERATE: 0 m 1 i 2 c 3 h 4 i 5 g 6 a 7 n Alphabet = michigan for c, value in enumerate(Alphabet): print(c, value) 2 m 3 i 4 c 5 h 6 i 7 g 8 a 9 n Alphabet = michigan for c, value in enumerate(Alphabet, 2): print(c, value)
STRING FORMATTING: 1- Using .format() method: price = 2000 str1= Price print('{ } is ${ }'.format(str1, price)) Price is $2000 #What gets printed? 2- Using fstrings method: print(f'{str1} is ${price }') Price is $2000 #What gets printed?
STRING FORMATTING 1- Using .format() method: { {:[align] [minimum_width] [.precision] [descriptor]} } >> price = 2000 >> print('{:>10,d}'.format(price) 2,000 |__________| 10 spaces #What gets printed? 2- Using fstrings method: { {variable:[align] [minimum_width] [.precision] [descriptor]} } >> print(f' {price:>10,d}') #What gets printed?
STRING FORMATTING s : string d : decimal integer f : floating point decimal Demo: Demo_Lab03.py LAB03 Part A Debugging: debug3.py