Skip to content

Problem 140 -- wrong solution? #3

@Randl

Description

@Randl

The canonical solution for problem 140 is given by

def fix_spaces(text):
    """
    Given a string text, replace all spaces in it with underscores, 
    and if a string has more than 2 consecutive spaces, 
    then replace all consecutive spaces with - 
    
    fix_spaces("Example") == "Example"
    fix_spaces("Example 1") == "Example_1"
    fix_spaces(" Example 2") == "_Example_2"
    fix_spaces(" Example   3") == "_Example-3"
    """
    ### Canonical solution below ###
    ans = text
    for i in range(len(text)-1, 2, -1):
        ans = ans.replace(" " * i, "-")
    return ans.replace(" ", "_")

Shouldn't it be len(text) instead?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions