Find duplicate letters in two different strings - python-3.x

My program asks the user to input first name then last name. How can I make it find the duplicate letters from first and last name? and if there is none it will print "no duplicate value in First and Last name"
here is my code but i need the output to be "the duplicate characters... is/are ['a', 'b', 'c']". and also when there is no duplicate my code prints multiple "No duplicate value in First name and Last name" but i need it to be one only.
for fl in firstName:
if fl in lastName:
print("The duplicate character in your First name and Last name is/are: ", tuple(fl))
else:
print("No duplicate value in First name and Last name")

One way to do so,
first_name = 'john'
last_name = 'Doe'
for letter in first_name:
if letter in last_name:
print(letter)

Related

What while loop condition will I use to detect the input all lowercase?

What while loop condition will I use to detect the input all lowercase?
FirstName = input("Enter your First Name: ")
LastName = input("Enter your Last Name: ")
while FirstName.lower() and LastName.lower():
print("Your First and Last Name should be typed in a lowercase letter")
FirstName = input("Enter your First Name: ")
LastName = input("Enter your Last Name: ")
print("Welcome")
Thank you for your help! Your help will be appreciated.
It always helps me to start out being very explicit in code about my intentions. Your intention is to keep looping until the user enters all lower case letters for their first and last names, so while not all_lower_case_entered keep looping. Inside your loop, your intention is to collect the user data and check to see if full_name.islower(). If not, then print your error message, if so, set all_lower_case_entered to True.
Example:
all_lower_case_entered = False
while not all_lower_case_entered:
first_name = input("Enter your first Name: ")
last_name = input("Enter your last Name: ")
full_name = first_name + last_name
if not full_name.islower():
print("Your first and last names should be typed in all lowercase letters.")
else:
all_lower_case_entered = True
print("Welcome")

How to switch order of last name and first name and remove coma

Might be a simple question but I am very new to Python.
If I have a given "Last Name, First Name", how would I switch the order to "First Name Last Name''?
For example, if I have:
"Doe, John"
How would I make it
"John Doe"?
I tried using .split(), but that converts it into a List and I want it to be a string.
EDIT: The suggested question gives a List, however my example is just a string
This is the code that I came up with:
def change_name(name: str) -> str:
x = name.split(',')
x.reverse()
x.join()
but that just gives me an error that the list doesn't have the attribute join
Also, I did str(x).join(' ') but that just gives me the List ['John', 'Doe']
EDIT: I seem to have gotten closer to what I want but it's still not perfect.
Now I have :
def change_name(name: str) -> str:
x = name.split(',')
x.reverse()
separator = ','
return separator.join(x)
which gives me
' John,Doe'
Now the problem is I need to take out the space in front of the string and the comma.
Your mistake is to use a comma to join the First Name and Last Name while you should have used a space. Try this:
>>> name = "Doe, John"
>>> x = name.split(', ')
>>> x.reverse()
>>> separator = ' '
>>> separator.join(x)
'John Doe'
The operations can also be chained in a single line, such as
>>> ' '.join(reversed(name.split(', ')))
'John Doe'

Why I am getting a duplicate output in Python?

I am trying the below code. But in the final output, I am getting repeated words. For example, if I input name as Jai, I will get JaiJai.
name = input ("Cheer: ")
for i in name:
name +=i
print('Give me a', i+",", i+"!")
print("What does it spell?")
print(name)
Because of this:
for i in name:
name +=i
for every character in a given word, add this character to the word.
You adding up the value of i to the name variable
this line name +=i is redundant here :)
corrected code:
name = input ("Cheer: ")
for i in name:
print('Give me a', i+",", i+"!")
print("What does it spell?")
print(name)

question tuples in regex -regular expressions

So I have some code on which I will use an regex on.
Specifically, I need to use re.findall() and a single regular expression to extract the three names and email addresses from the 'string'. To create list of 3 tuples like so: [('Mary Boe', 'md90#uw.com'), ('Cheri Moe Drake', 'cmd39#gmail.gbl'), ('R.L. Fitzgeri', 'fit.rl#hotmail.ing')]
here is the string....
string = """Name: Mary Boe, Email: md90#uw.com\n
Name: Cheri Moe Drake, Email: cmd39#gmail.gbl\n
Name: R.L. Fitzgeri, Email: fit.rl#hotmail.ing"""
So far I have used the following to get ['R.L. Fitzgeri']
with
re.findall('\S\S\w\S\s\w\S\w\w\w\w\S\w',string)
And I have been able to get fit.rl#hotmail.ing
with
re.findall('\w\\w\\w\\S\w\w\S\w\w\w\w\S\w\w\S\w\w\w',string)
I have been able to get Cheri Moe Drake with
re.findall('\w\w\w\w\w\s\w\w\w\s\w\w\w\w\w',string)
But I have struggled condensing this, and secondly, struggled to get it so that it all comes out, as I said, like:
[('Mary Boe', 'md90#uw.com'), ('Cheri Moe Drake', 'cmd39#gmail.gbl'), ('R.L. Fitzgeri', 'fit.rl#hotmail.ing')]
Here is a way to do the job:
import re
string = """Name: Jane Doe, Email: jd12#uw.com\n
Name: Sally Sue Draper, Email: ssd59#gmail.edu\n
Name: J.D. Salinger, Email: sal.jd#hotmail.org"""
pattern = r'Name: (.+?), Email: (.+)'
result = re.findall(pattern, string)
print(result)
Output:
[('Jane Doe', 'jd12#uw.com'), ('Sally Sue Draper', 'ssd59#gmail.edu'), ('J.D. Salinger', 'sal.jd#hotmail.org')]
Regex explanation:
Name: # literally
(.+?) # group 1, 1 or more any character but newline, not greedy
, Email: # literally
(.+) # group 2, 1 or more any character but newline
If you always have the same format, it may make more sense to avoid regular expressions in this scenario and approach the problem alternatively:
string = """Name: Jane Doe, Email: jd12#uw.com\n
Name: Sally Sue Draper, Email: ssd59#gmail.edu\n
Name: J.D. Salinger, Email: sal.jd#hotmail.org"""
people = [person for person in string.split('\n') if person]
people_list = []
for person in people:
name = ''
for char in person[6:]:
if char == ',':
break
else:
name += char
email = ''
for char in person[::-1]:
if char == ' ':
break
else:
email += char
email = email[::-1]
person_tuple = (name, email)
people_list.append(person_tuple)
This will give you a list of tuples if you print people_list:
[('Jane Doe', 'jd12#uw.com'), ('Sally Sue Draper', 'ssd59#gmail.edu'), ('J.D. Salinger', 'sal.jd#hotmail.org')]
This assumes that all your lines start with Name:, which is why the loop builds a name by concatenating all characters after that up to the first comma it finds.
For the email, it does the same thing in reverse: it takes all characters starting from the end of the string until it finds a space, where the email is effectively ending. It then puts it back in order to get the correct email.
To build the list of contacts, the loop will format name and email into a tuple that will be appended to people_list until there are no more contacts to add.
If you insist on using regular expressions, then a good use for those could be if you want to validate email addresses and not add a contact to your list if the email does not correspond to the format of your choosing (or leave it blank instead). The regex pattern could look like this:
email = 'example#email.com'
pattern = r'[a-z]+[a-z0-9]*[\w._-]*#[a-z]+\.[a-z]{1,3}$'
if re.match(pattern, email):
# do something with email here
Note that in this case, the regex uses symbols like + and * to avoid repetition, which is one of the keys in building a more robust regex.

Searching Names and phonenumbers

mine is homework question in response to the previous Question i posted on this site:
i redid the code to the following:
import re
people = ["Karen", "Peter", "Joan", "Joe", "Carmen", "Nancy", "Kevin"]
phonenumbers = ["201-222-2222", "201-555-1212", "201-967-1490", 201-333-3333",'201-725-3444", "201-555-1222", "201-444-4656"]
name = raw_input("Enter person's name:")
found = false
for i in range(0, len(people)):
value = people[i]
m = ("(" + name + ".*)",value)
if m:
found = True
print (people[i], phonenumber[i])
else:
print ("No matching name was found.")
My question is how do i tell the program to check if Karen's phone number is the 201-222-2222? And Yes this is a homework assignment. I changed the names and the phone numbers in my acutal program.
When i run this program and type any character all the names and phone number show up that's where i'm having difficutly...
EDITED: Question isn't clear to me.
The following code my help.
1.) First it ask for name then check if it exist in the people list.
2.) Then if it exist it saves it in variable called abc.
3.) After loop is finished it prints the abc which is the name you entered and that person phone number.
import re
people = ["Karen", "Peter", "Joan", "Joe", "Carmen", "Nancy", "Kevin"]
phonenumbers = ["201-222-2222", "201-555-1212", "201-967-1490", "201-333-3333","201-725-3444", "201-555-1222", "201-444-4656"]
name = input("Enter person's name:")
abc = "" # Will store the name and phone number
found = False
for i in range(0, len(people)):
if people[i].lower() == name.lower(): #checks if input name match + use method to lower all char
abc = people[i]+" Has the number "+phonenumbers[i]
value = people[i]
m = ("(" + name + ".*)",value)
if m:
found = True
print (people[i], phonenumbers[i]) # missing letter "s"
else:
print ("No matching name was found.")
print("\n"+abc)
Result

Resources