DEV Community

Cover image for python challenge_6
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on • Edited on

python challenge_6

Adding and removing dots

level of challenge = 3/10

  • Write a function named add_dots that takes a string and adds "." in between each letter.
  • For example
  • calling add_dots("test") should return the string "t.e.s.t".

  • Then, below the add_dots function,

  • write another function named remove_dots that removes all dots from a string.

  • For example

  • calling remove_dots("t.e.s.t") should return "test".

  • If both functions are correct,

  • calling remove_dots(add_dots(string)) should return back the original string for any string.

Hint

  • For add_dots you may find the string join method useful.
  • If you don't want to use it,
  • You can instead iterate over each letter in,
  • The input while building up a result string that is initially empty.

  • The remove_dots function is similar.

  • Either use the string replace method or manually loop over the letters,

  • keeping ones that aren't ".".

My solution

def add_dots(string):
    new_string = ".".join(string)
    return new_string
print(add_dots("test"))

def remove_dots(string):
    new_string = string
    result = new_string.replace(".", "")
    return result
print(remove_dots("t.e.s.t"))

print(remove_dots(add_dots("tito")))
Enter fullscreen mode Exit fullscreen mode

Another solution

the longer way

def add_dots(s):
    out = ""
    for letter in s:
        out += letter + "."
    return out[:-1] # -> do not show the lest dot 

def remove_dots(s):
    out = ""
    for letter in s:
        if letter != ".":
            out += letter
    return out
Enter fullscreen mode Exit fullscreen mode

the short way

def add_dots(s):
    return ".".join(s)

def remove_dots(s):
    return s.replace(".", "")
Enter fullscreen mode Exit fullscreen mode

Add your solution in the comment :)

Top comments (8)

Collapse
 
elnaznasiri profile image
elnaz-nasiri • Edited

My solution is:

def add_dots(string):
    newString = '.'.join(string)
    return newString

print(add_dots("test"))
Enter fullscreen mode Exit fullscreen mode
def remove_dots(string):
    splitString = string.split(".")
    newString = "".join(splitString)
    return newString


print(remove_dots("t.e.s.t"))
Enter fullscreen mode Exit fullscreen mode
print(remove_dots(add_dots("test")))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Thanks for sharing you can test your solution here: pythonprinciples.com/challenges/

Collapse
 
alvisonhunter profile image
Alvison Hunter Arnuero | Front-End Web Developer
def add_dots(string):
    return ".".join(string)

def remove_dots(string):
    return string.replace(".", "")

print("With Dots: ", add_dots("test"))  # t.e.s.t
print("Without Dots: ", remove_dots("t.e.s.t"))  # test
print("Back to Original: ", remove_dots(add_dots("alvison"))) # alvison
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Good job pro :)

Collapse
 
alvisonhunter profile image
Alvison Hunter Arnuero | Front-End Web Developer

Thanks, champion!

Thread Thread
 
mahmoudessam profile image
Mahmoud EL-kariouny

Not at all :)

Collapse
 
shapeshapa profile image
shapa • Edited

I would prefer to use an extended class of string, it wil be much more reusable.

class Dots(str):
    def __init__(self, value: str):
        self.value = value

    def add_dots(self):
        return self.__class__(".".join(self.value))

    def remove_dots(self):
        return self.__class__(".".replace(".", ""))

msg = Dots("Hello")
print(msg.add_dots())
print(msg.add_dots().lower())
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny • Edited

It is very good, But in my case I did have to follow the instructions
this is link of all challenges I work on it : pythonprinciples.com/challenges/