Note that there are some explanatory texts on larger screens.

plurals
  1. POReplacing chars in a string in every way
    text
    copied!<p>I'm looking for help on a function that takes a string, and replaces every character in that string in every way. I'm not quite sure how to word my question so that it makes sense so I'll show you what it's supposed to do.</p> <pre><code>stars('1') returns ['*'] stars('12') returns ['*1', '1*', '**'] stars('123') returns ['*23', '1*3', '12*', '**3', '*2*', '**1', '***'] stars('1234') returns ['*234', '1*34', '12*4', '123*', '**34', '*2*4', '*23*', '1**4', '1*3*', '12**', '***4', '**3*', '*2**', '1***', '****'] </code></pre> <p>Did that all out by hand, but even if I made a mistake, you should get the idea of what I'm looking for now. The final case (all *'s) isn't required but I put it in there to make sure the problem was understood.</p> <p>Here is what I've come up with so far but it doesn't quite work.</p> <pre><code>def stars(n): lst = [] length = len(n) for j in xrange(0, length): p = list(n) for k in xrange(j, length): p[k] = '*' lst += [''.join(p)] return lst </code></pre> <p>Output:</p> <pre><code>'1' returns ['*'] '12' returns ['*2', '**', '1*'] '123' returns ['*23', '**3', '***', '1*3', '1**', '12*'] '1234' returns ['*234', '**34', '***4', '****', '1*34', '1**4', '1***', '12*4', '12**', '123*'] </code></pre> <p>Any help would be greatly appreciated. Would like this answered in Python if possible, but if you don't know Python, then pseudocode or another language would be acceptable. If it's written clearly, I'm sure I could convert it into Python on my own.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload