Sharing Solutions

def make_spoonerism(word1, word2):
  swap_first_letters = (word2[0] + word1[1:], word1[0] + word2[1:])
  single_string_swapped = ' '.join(swap_first_letters)
  return single_string_swapped

Well it isn’t pretty but it did the job. After viewing the solution it makes me laugh how simplified it could have been. I basically had it in line 2. I need to keep practising to find an easier way to solve issues.

My way of doing this, not as simple as a solution but it works.

Here’s my attempt. Works and based on how well I don’t do on these exercises, I’m happy enough with this.

def make_spoonerism(word1, word2):
return word2[0] + word1[1:] + " " + word1[0] + word2[1:]

2 Likes

This is the same solution I came up with.

here’s mine solution:

def make_spoonerism(word1, word2):
a = word2[0] + word1[1:]
b = word1[0] + word2[1:]
return(a+" "+b)

At first I went the longest possible way about this, realised there must be an easier way and felt stupid:

def make_spoonerism(word1, word2):
  new_word1 = ""
  new_word2 = ""
  word1_list = []
  word2_list = []
  for each in range(len(word1)):
    word1_list.append(word1[each])
  for each in range(len(word2)):
    word2_list.append(word2[each])
  word1_0 = word1_list[0]
  word2_0 = word2_list[0]
  word1_list[0] = word2_0
  word2_list[0] = word1_0
  for each in range(len(word1_list)):
    new_word1 = new_word1 + word1_list[each]
  for each in range(len(word2_list)):
    new_word2 = new_word2 + word2_list[each]
  return str(new_word1) + " " + str(new_word2)

then I did this and felt less stupid:

def make_spoonerism(word1, word2):
  swap_first_letters = word2[0] + word1[1:len(word1)] + " " + word1[0] + word2[1:len(word2)]
  return swap_first_letters

Then i realised that word[1:len(word1)]* was unnecessary.

The len(word1) part, anyway.

word[1:]

i used replace is it right to use it ?

def make_spoonerism(word1, word2):
letter1=word1[0]
letter2=word2[0]
worda=word1.replace(letter1, letter2)
wordb=word2.replace(letter2, letter1)
return worda+" "+wordb

That would depend upon the instructions given. Is this a freeform project or a lesson? Sometimes lesson checkers have expectations as to code patterns and variable names.

It was a code challenge, I didn’t even check the given hint, but followed the instructions to get the final result as it said it got to be

What if the there is more than one letter matching the first letter in the word?

In that case my code won’t work and will have to change it :confused:

1 Like

So there is your answer. Be sure to test your solution against the possibility of duplicate letters in the words.

Is my code correct ok? or should i find a better solution? For the exercise is correct but i don’t know if for day to day coding still ok is:

def make_spoonerism(word1, word2):
  word1_replace = word1.replace(word1[0], word2[0])
  word2_replace = word2.replace(word2[0], word1[0])
  return word1_replace + " " + word2_replace

What does str.replace do, does that match what you’re doing with it? How many replacements does str.replace do, and how many do you want to happen?

It’s not pretty but it does the job:

def make_spoonerism(word1, word2):
  new = ""
  new += word2[0] 
  new += word1[1:] 
  spacja = " "
  new += spacja 
  new += word1[0] 
  new += word2[1:] 
  return new

def make_spoonerism(word1,word2):
word1_fixed = word1.replace(word1[0],word2[0])
word2_fixed = word2.replace(word2[0],word1[0])
print(word1_fixed,word2_fixed)

so this was what I was attempting but it keeps returning None after it prints each word, I know I haven’t combined the strings yet but I was just curious as to why it is returning None at all if anyone could help I would appreciate it

You forgot to add return in the bottom of your function, None is returned by default if return is not included in your function.

Change this

def make_spoonerism(word1,word2):
  word1_fixed = word1.replace(word1[0],word2[0])
  word2_fixed = word2.replace(word2[0],word1[0])
  print(word1_fixed,word2_fixed)

To

def make_spoonerism(word1,word2):
  word1_fixed = word1.replace(word1[0],word2[0]) 
  word2_fixed = word2.replace(word2[0],word1[0])
  return word1_fixed + ' ' + word2_fixed

Hopefully this helps. :slight_smile:

def make_spoonerism(word1, word2):
  abc = ""
  bcd = ""
  for i in word1:
    abc = word1[0] + word2[1:]
  for y in word2:
    bcd = word2[0] + word1[1:]
  return bcd +" "+ abc

This was my solution

The loops don’t do anything beyond the first iterations. You could remove them and then in-line a couple of things.