You aren't exactly doing anything recursively if all you do is call the function and store the value in ListIn2
. You will keep overwriting your previous data. By returning, you will end up getting your recursive behaviour:
def RecursiveProcess(ListIn2, target): #Recursive
function that adds to target value the value to its right
if target > -1: #stop
function
if index is below 0
ListIn2[target] = ListIn2[target] + ListIn2[target + 1] #Add value to the right of target to the target value
return RecursiveProcess(ListIn2, target - 1) #Call the
function again with lower taget value to process the next value
else:
return ListIn2 #return the changed list
l = [5, 10, 11, 6, 7, 1, 2, 4, 6, 7]
d = RecursiveProcess(l, len(l) - 2)
print(d) #[59, 54, 44, 33, 27, 20, 19, 17, 13, 7]
The problem is that you don't actually do a recursion (every call to the same function returns the result) but since lists are mutable you don't need to:
def ProcessList(ListIn):
RecursiveProcess(ListIn, len(ListIn) - 2) #this will change the list in place!
return ListIn
that's all to get it working like expected. Because each element is updated in the "recursion" so there is no need to pass the pointer to the list around. And also the result is what is expected:
#[59, 54, 44, 33, 27, 20, 19, 17, 13, 7]
You aren't returning in your general case. Try:
def RecursiveProcess(ListIn2, target):
if target > -1:
ListIn2[target] = ListIn2[target] + ListIn2[target + 1]
ListIn2 = RecursiveProcess(ListIn2, target - 1)
return ListIn2 #Actually need to
return in the general
case
else:
return ListIn2
The Python file below includes a function named remove_duplicates_recursion which takes two arguments: dupList and temp. dupList is the list possibly containing duplicate elements, and temp is initially an empty list.,Since the length of dupList is >= 1, and duplist[0] is not in temp, the block under condition # 2 will execute. It will add the first element of dupList in the list to return and call the remove_duplicates_recursion function again with dupList = [1, 2, 2] and temp = [1].,As the function proceeds recursively, it will not allow any element in temp to be stored in the list that it has to return, and temp will keep filling up with successive elements in dupList.,If the element is present then it will be appended at the end of temp and the function will be recursively called without the first element of dupList.
def remove_duplicates_recursion(dupList, temp):
if len(dupList) == 0: #condition 0-- > base
case
return dupList
if dupList[0] in temp: #condition 1
temp.append(dupList[0])
return remove_duplicates_recursion(dupList[1: ], temp)
else: #condition 2
temp.append(dupList[0])
return [dupList[0]] + remove_duplicates_recursion(dupList[1: ], temp)
def main():
nums = [1, 1, 2, 2]
strings = ['hello', 'world', 'hello', 'world', 'hi']
emptyList = []
print("number list with duplicates", nums)
print("number list without duplicates", remove_duplicates_recursion(nums, emptyList))
print("strings list with duplicates", strings)
print("string list without duplicates", remove_duplicates_recursion(strings, emptyList))
main()
Last Updated : 20 Jul, 2022
Examples:
Input: aaaaabbbbbb
Output: ab
Input: geeksforgeeks
Output: geksforgeks
Input: aabccba
Output: abcba
The recursion tree for the string S = aabcca is shown below.
aabcca S = aabcca /
abcca S = abcca /
bcca S = abcca /
cca S = abcca /
ca S = abca /
a S = abca(Output String) /
empty string
geksforgeks abca
geksforgeks abca
© 2007—2022 Ilya Kantor
pow(2, 2) = 4 pow(2, 3) = 8 pow(2, 4) = 16
function pow(x, n) {
let result = 1;
// multiply result by x n times in the loop
for (let i = 0; i < n; i++) {
result *= x;
}
return result;
}
alert(pow(2, 3)); // 8
function pow(x, n) {
if (n == 1) {
return x;
} else {
return x * pow(x, n - 1);
}
}
alert(pow(2, 3)); // 8
if n == 1 = x /
pow(x, n) = \
else = x * pow(x, n - 1)
function pow(x, n) {
return (n == 1) ? x : (x * pow(x, n - 1));
}
function pow(x, n) {
if (n == 1) {
return x;
} else {
return x * pow(x, n - 1);
}
}
alert(pow(2, 3));
If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.,Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.,This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL),Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
protected void btnSave_Click(object sender, EventArgs e) {
string result = string.Empty;
string FacilityId = string.Empty;
int current;
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// var random0 = new Random();
result = new string(
Enumerable.Repeat(chars, 1)
.Select(s => s[random.Next(s.Length)])
.ToArray());
lock(syncLock) { // synchronize
current = random.Next(10000, (99999));
FacilityId = result + current;
bool duplicate = DuplicateCheck(FacilityId);
if (duplicate == true) {
current = random.Next(10000, (99999));
FacilityId = result + current;
bool duplicate1 = DuplicateCheck(FacilityId);
}
}
}
private bool DuplicateCheck(string FacilityId) {
bool Flag = false;
var duplicate = objDB.SelectAll().Where(a => a.FacilityID == FacilityId).ToList();
if (duplicate.Count > 0) {
current = random.Next(10000, (99999));
FacilityId = result + current;
Flag = true;
}
return Flag;
}
current = random.Next(10000, 99999);
FacilityId = result + current;
while (objDB.SelectAll().Any(a => a.FacilityID == FacilityId)) {
current = random.Next(10000, 99999);
FacilityId = result + current;
}
string GetNextFacilityId() {
lock(syncLock) {
string highestFacilityIdInUse =
objDB.SelectAll() // see note below
.OrderByDescending(a => a.FacilityID)
.Select(a => a.FacilityID)
.Take(1)
.SingleOrDefault();
string nextFacilityId;
if (highestFacilityIdInUse == null)
nextFacilityId = "A10000";
else {
// omitted here: checking highestFacilityIdInUse for correct format
// [A-Z][10000-99999]
char c = highestFacilityIdInUse[0];
int n = Int32.Parse(highestFacilityIdInUse.Substring(1));
if (c == 'Z' && n == 99999)
throw new InvalidOperationException("ran out of FacilityIds");
else if (n == 99999)
nextFacilityId = (char)(c + 1) + "10000";
else
nextFacilityId = c + (n + 1).ToString();
}
return nextFacilityId;
}
}
string GetNextFacilityId() {
lock(syncLock) {
Random rnd = new Random();
string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char c = chars[rnd.Next(chars.Length)]; // simpler than your Enumerable-approach
int n = rnd.Next(10000, 100000); // 2nd value is EXCLUSIVE upper bound!
string nextFacilityId = c + n.ToString();
while (objDB.SelectAll().Any(a => a.FacilityID == nextFacilityId)) {
c = chars[rnd.Next(chars.Length)];
n = rnd.Next(10000, 100000);
nextFacilityId = c + n.ToString();
}
return nextFacilityId;
}
}