Created by Tyler Burnham
Are problems that deal with parsing strings. They can be:
“On October 24th, 2014, Britain's Queen Elizabeth II dipped a toe into 21st-century communications when she posted her first tweet. Signing herself Elizabeth R., for regina or queen, she welcomed visitors to a new Information Age gallery focused on the evolution of modern communications at the Science Museum in London. The inaugural tweet (shown above) was posted to the official British monarchy Twitter feed, which has more than 700,000 followers.”
“Your job is to write a program to scan a given number of tweets for how many mention the at symbol to the British Monarchy, @BritishMonarchy. The username following the @ symbol in Twitter is case insensitive.”
4
So excited that we can now tweet The Queen @bRitishMonaRchY!! :-)
@BritishMonarchy #Christmas is 6 weeks away!! #soClose
Russ Rose would like to wish you all a Happy #Thanksgiving. #SNL
@BritishMonarchy’s great grandson, heir to throne of @BritishMonarchy
Total Tweets Containing @BritishMonarchy = 3
import java.util.Scanner;
import java.util.StringTokenizer;
public class RoyalTweet{
public static void main(String []args){
Scanner input = new Scanner(System.in);
int numberOfLines = Integer.parseInt(input.nextLine().toString());
int count = 0;
for(int i = 0; i < numberOfLines; i++)
{
String line = input.nextLine();
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreElements())
{
String word = st.nextElement().toString();
if(word.toLowerCase().contains("@britishmonarchy"))
{
count ++;
break;
}
}
}
System.out.println("Total Tweets Containing @BritishMonarchy =" + count);
}
}
count = 0
for x in range(int(input())):
if input().lower().count("@britishmonarchy"):
count += 1
print("Total Tweets Containing @BritishMonarchy = " + str(count))
print("Total Tweets Containing @BritishMonarchy = " + str(
sum([1 if input().lower().count("@britishmonarchy") else 0
for x in range(int(input()))]
)))
>>> "Binary Bears"[:3]
'Bin'
>>> "Binary Bears"[3:]
'ary Bears'
>>> "Binary Bears"[3:4]
'a'
>>> "Binary Bears"[::2]
'Bnr er'
>>> "Binary Bears"[::-1]
'sraeB yraniB'
>>> l = [1, 2, 3, 4]
>>> [num+1 for num in l]
[2, 3, 4, 5]
>>> ["Case " + str(num) for num in l]
['Case 1', 'Case 2', 'Case 3', 'Case 4']
#Change Case
"BINARY BEARS".lower() -> 'binary bears'
"binary bears".upper() -> 'BINARY BEARS'
"binary BEARS".swapcase() -> 'BINARY bears'
#Other Change Case
"binary bears".capitalize() -> 'Binary bears'
"binary bears".title() -> 'Binary Bears'
#Case Conditionals
"binary bears".isupper() -> False
"binary bears".islower() -> True
"binary bears".istitle() -> False
#Find
"binary bears".find('a') = 3
"binary bears".rfind('a') = 9 #Return the highest index
"binary bears".index('bear') = 7 #Like find(), but raise ValueError when the substring is not found.
"binary bears".rindex('a') = 9
#In
"bear" in "binary bears" = True
#Count
"binary bears".count('bear') = 1
str.isalpha()
str.isnumeric()
str.isdecimal()
str.isdigit()
str.isalnum()
str.isspace() #" ","\t","\n","\r","\f"
String Python Docs
str.isnumeric() vs str.isdecimal() vs str.isdigit()
>>> "Red fish, Blue Fish, Green Fish".split(',')
['Red fish', ' Blue Fish', ' Green Fish']
>>> "Red fish, Blue Fish, Green Fish".split(',', maxsplit=1)
['Red fish', ' Blue Fish, Green Fish']
>>> "Line1\nLine2\nLine3\n".splitlines()
['Line1', 'Line2', 'Line3']
>>> "Line1\nLine2\nLine3\n".splitlines(keepends=True)
['Line1\n', 'Line2\n', 'Line3\n']
" B e a r ".strip() = 'B e a r'
" Bear ".lstrip() = 'Bear '
" Bear ".rstrip() = ' Bear'
>>> "1".zfill(2)
'01'
>>> "123".rjust(6)
' 123'
>>> "123".ljust(6)
'123 '
>>> "123".center(6)
' 123 '
>>> "{0:.1f}".format(123.15)
'123.2'
import string
string.ascii_letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
string.ascii_lowercase = "abcdefghijklmnopqrstuvwxyz"
string.ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
string.digits = "0123456789"
string.hexdigits = "0123456789abcdefABCDEF"
string.octdigits = "01234567"
string.punctuation = '!"#$%&''()*+,-./:;<=>?@[\]^_`{|}~'
string.printable
string.whitespace