Filling the missing values in specified format using python

import re
# you can free to change all these codes/structure
def curve_smoothing(string):
    blank_count=0
    value_count = 0
    splitString = string.split(",")
    print("Input:",string)
    #print(string)
    totalNumbers = len(splitString)
    for i in splitString:
        if i=="_":
            blank_count+=1
        else:
            value_count=int(i)+int(value_count)
    sequenceNumber = int(value_count/totalNumbers)
    if(blank_count!=0):
        output = string.replace("_",str(sequenceNumber),blank_count)
    print(output)

for a given input : S2="_,_,_,24"
expected output = 6,6,6,6
But my program gives output = 6,6,6,24

How do i fix it?

By telling it to overwrite not just the blanks but the original values too.

that says “replace _ with the number, blank number of times”
What you actually want it to do is create a new string, which is the number of inputs long, filled with the value you have determined, with commas inbetween.

yes that works, but there is one caveat in my code.

Input: _,_,_,24
6,6,6,6
Input: 40,_,_,_,60
20,20,20,20,20
Input: 80,_,_,_,_
16,16,16,16,16
Input: _,_,30,_,_,_,50,_,_
8,8,8,8,8,8,8,8,8

But my expected output should be

Input1: "_,_,_,24"
Output1: 6,6,6,6

Input2: "40,_,_,_,60"
Output2: 20,20,20,20,20

Input3: "80,_,_,_,_"
Output3: 16,16,16,16,16

Input4: "_,_,30,_,_,_,50,_,_"
Output4: 10,10,12,12,12,12,4,4,4

Fr the input4, my result is not as expected. what should be in my logic to fix it.

my code
import re

def curve_smoothing(string):
    blank_count=0
    value_count = 0
    splitString = string.split(",")
    print("Input:",string)
    #print(string)
    totalNumbers = len(splitString)
    for i in splitString:
        if i=="_":
            blank_count+=1
        else:
            value_count=int(i)+int(value_count)
    
    sequenceNumber = int(value_count/totalNumbers)
    lst1=[]
    for i in range(totalNumbers):
        lst1.append(str(sequenceNumber))
    print(','.join(lst1))

Okay… inhales let’s teach you about code tags. :stuck_out_tongue:

If you’re posting code to the forum, wrap it in triple backticks.

```
my code
goes here
```

and the forum will render it correctly

my code
goes here

Trying to read your code in base formatting is screwy because you’re using a lot of underscores which are a forum special character.

Ok, I will do that.

backtick, not apostrophe, and put them on separate lines from your code.

Thank you :slight_smile: so much easier to read!

Can you tell me why you expect your output 4 to be what you have posted? Your code is doing what you told it to do, which means you haven’t applied whatever formula you’re trying to apply correctly. So I need to know what formula you’re trying to apply.

Ex 4: _, _, 30, _, _, _, 50, _, _  
==> we will fill the missing values from left to right 
    a. first we will distribute the 30 to left two missing values (10, 10, 10, _, _, _, 50, _, _)
    b. now distribute the sum (10+50) missing values in between (10, 10, 12, 12, 12, 12, 12, _, _) 
    c. now we will distribute 12 to right side missing values (10, 10, 12, 12, 12, 12, 4, 4, 4)

Ahhhh okay so it’s a lot more complex than simply doing an average.

Okay. So, let’s break down your steps a little bit more, and see if we can get to what you need to do:

a. first we will distribute the 30 to left two missing values (10, 10, 10, _, _, _, 50, _, _)
So tell me, if you know nothing about your input, what does this actually mean you need to do? How do you start working through the input? And for that matter, at what point(s) do you need to stop working through the input and do some processing?

   def curve_smoothing(string):
   blank_count=0
   value_count = 0
   splitString = string.split(",")
   print("Input:",string)
   #print(string)
   totalNumbers = len(splitString)
   for i in splitString:
       if i=="_":
           blank_count+=1
       #elif(i!="_"):
       else:
            value_count=int(i)+int(value_count)
       sequenceNumber = int(value_count/totalNumbers)
       lst1=[]
       for i in range(totalNumbers):
           lst1.append(str(sequenceNumber))
       lst2=','.join(lst1)

I am trying to count the number of blanks until a number is encountered. Then divide the number according to the number of blanks.
Then it is the same process until next number is encountered. But the loop should be from the current element again and then it should be added to the next encountered number.

In the above I am unable to loop from the current element again. How is this achieved?

I have pocketed away my own solution to this, because you’re working on yours.

If you do all of your processing inside the else:, you are effectively pausing the loop until you finish processing. So do your processing inside the else.

Example:

for i in splitString:
   print i
   if i=="_":
     print "A"
   else:
     print "B"
     value = 90000
     dosomestuff
     print "More Things!"
     for j in anotherthing:
        print "I'm a loop inside a loop. When i'm done, i will still be the same value."
   print "I get run after every loop of i"

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.