Jump to content

Help with python coding problem

johnadams123412

Are you supposed to write these functions yourself or use built ins? The first one is a built in function ('title()').

For the second one you can just compare the lengths and return the largest one. The length of a string s is just

len(s)

 

PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Gigabyte RTX 3060TI Gaming OC ProFractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 2TB WD BlueCorsair RM850x, LG 27GL83A-B

Link to comment
Share on other sites

Link to post
Share on other sites

For the first one, you can use the .upper and .lower methods and concatenate it together OR use the title function.

 

For the second you can use the len function

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, martward said:

Are you supposed to write these functions yourself or use built ins? The first one is a built in function ('title()').

For the second one you can just compare the lengths and return the largest one. The length of a string s is just


len(s)

 

write these functions myself, its a practice problem im doing in a course so for long name i have this lol but i could be completely off

def long_name(name_1, name_2):

names = (name_1, name_2)

for words in names:  

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

The way I tackle these problems is by first starting out writing the assignment in steps and then find out how to do these steps.

Keep in mind I do not know Python, so will just write it out in C#-like 'code' (this is really just pseudocode), but it will give you some ideas to Google.

private string title(string input)
{
 	// Check if the word adheres to the standard (the capatilized stuff, possible optional)
  	// it's probably easiest now to split the word in two parts: first letter and last letter(s). make the first part uppercase, rest lowercase. see link below. 
  
 	// Now return that change input
}

private string long_name(string name1, string name2)
{
	// Compare the length of the names. Easiest to just get the length of either string and compare that, kind like this:
  	int name1Length = len(name1);
  	int name2Length = len(name2);
  	
  	// Is name 1 a higher number??
  	if(name1Length > name2Length)
    {
      return name1;
    }
  	else
    {
      return name2;
    }
}

https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/

https://www.geeksforgeeks.org/python-string-length-len/

 

You're gonna have to check yourself though how to take parameters, create functions, return stuff, how to exactly do the upper/lower case stuff.. etc.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, IshGames said:

write these functions myself, its a practice problem im doing in a course so for long name i have this lol but i could be completely off

def long_name(name_1, name_2):

names = (name_1, name_2)

for words in names:  

 

 

Okay so I wouldn't necessarily make a tuple of the inputs, since you will always have exactly two names. You could do something like:d

def long_name(name1, name2):
  if len(name1) >= len(name2):
    return name1
  else:
    return name2

This will return name1 if it is longer or as long as name 1 otherwise it returns name2.

PSU tier list // Motherboard tier list // Community Standards 

My System:

Spoiler

AMD Ryzen 5 3600, Gigabyte RTX 3060TI Gaming OC ProFractal Design Meshify C TG, 2x8GB G.Skill Ripjaws V 3200MHz, MSI B450 Gaming Plus MaxSamsung 850 EVO 512GB, 2TB WD BlueCorsair RM850x, LG 27GL83A-B

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, IshGames said:

write these functions myself, its a practice problem im doing in a course so for long name i have this lol but i could be completely off

def long_name(name_1, name_2):

names = (name_1, name_2)

for words in names:  

 

 

That doesn't actually do anything yet so... you're neither wrong nor right. You could just write

for name in (name1, name2):
	...

instead of creating an extra variable though.

 

With this kind of exercise it's hard to help you without doing it for you, @Slottr gave you some pretty good hints so I'd start with those.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, martward said:

Okay so I wouldn't necessarily make a tuple of the inputs, since you will always have exactly two names. You could do something like:d


def long_name(name1, name2):
  if len(name1) >= len(name2):
    return name1
  else:
    return name2

This will return name1 if it is longer or as long as name 1 otherwise it returns name2.

oh that makes it so much easier, just comparing the names and returning the name with bigger length, i dont know why i was overthinking it,
Any ideas on the capitalizing the first letter of a word thats jumbled like "JoHn" and lowercasing the rest?
im assuming youd do like .higher(0) and lower(?) idk im lost on that one

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, IshGames said:

oh that makes it so much easier, just comparing the names and returning the name with bigger length, i dont know why i was overthinking it,
Any ideas on the capitalizing the first letter of a word thats jumbled like "JoHn" and lowercasing the rest?
im assuming youd do like .higher(0) and lower(?) idk im lost on that one

Bruv

 

17 minutes ago, Slottr said:

For the first one, you can use the .upper and .lower methods and concatenate it together OR use the title function.

 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Slottr said:

Bruv

 

 

Sorry I read your comment after I replied, I was trying to work it out, thats why i initially said .higher, then read your comment and realized im stupid and its .upper, coding is hard not gonna lie

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, IshGames said:

Sorry I read your comment after I replied, I was trying to work it out, thats why i initially said .higher, then read your comment and realized im stupid and its .upper, coding is hard not gonna lie

lol all good

 

It'll get easier the higher up you go. All this little stuff will become natural in no time. Make sure to challenge yourself though. Don't ask for a solution, but ask for help towards a solution. It's hard, but it helps immensely 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Slottr said:

lol all good

 

It'll get easier the higher up you go. All this little stuff will become natural in no time. Make sure to challenge yourself though. Don't ask for a solution, but ask for help towards a solution. It's hard, but it helps immensely 

so far I have

def title(word)

then im assuming i have to do .upper(0)  idk. and lower case rest of word? how would I go about it?

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Slottr said:

lol all good

 

It'll get easier the higher up you go. All this little stuff will become natural in no time. Make sure to challenge yourself though. Don't ask for a solution, but ask for help towards a solution. It's hard, but it helps immensely 

or could i use word.title() command?

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, IshGames said:

so far I have

def title(word)

then im assuming i have to do .upper(0)  idk. and lower case rest of word? how would I go about it?

You can use indexing

 

So if you want everything but the first letter, you can reference it like this:

 

string[1:]

and use the .lower method on that

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, Slottr said:

You can use indexing

 

So if you want everything but the first letter, you can reference it like this:

 


string[1:]

and use the .lower method on that

i got it thanks so much for your help

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×