Haskell Functional Programming Language:
Write a function nestedParens that takes a string argument and returns true if it is a nesting of zero or more pairs of parentheses, e.g. "((()))" should return True ; "()()" or "(()))" should return False . Use recursion for this problem.
nestedParens :: String -> Bool
Code in c++ for above problem-
#include <iostream>
using namespace std;
int nestedParens(string S)
{
int current_max = 0; // current count
int max = 0; // overall maximum count
int n = S.length();
for (int i = 0; i< n; i++)
{
if (S[i] == '(')
{
current_max++;
if (current_max> max)
max = current_max;
}
else if (S[i] == ')')
{
if (current_max>0)
current_max--;
else
return -1;
}
}
// finally check for unbalanced string
if (current_max != 0)
return -1;
return max;
}
int main()
{
string s = "()()";
if(nestedParens(s)==1){
cout<<"false";
}else{
cout<<"true";
}
return 0;
}
Haskell Functional Programming Language: Write a function nestedParens that takes a string argument and returns true...
Please write below functions in HASKELL functional programming language and remember to include both the (1) function declaration, and (2) function definition. 1. Write a function sumLastPart which, only using library functions, returns the sum of the last n numbers in the list, where n is the first argument to the function. sumLastPart :: Int -> [Int] -> Int
Write a function check palindrome, which takes a string x as argument and which returns True if x is a palindrome, and False otherwise. A palindrome is a word that reads the same backwards as forwards (like for example “racecar”). Your function should be recursive (i.e. call itself) and proceed as follows. 1. If x is a string of length 0 or 1 return True. 2. If the rst and the last letter of x are di erent, return False....
C++ Programming: Write a function that takes in a C++-style string s (from #include ), as well as an integer n, and then returns true if any substring of length n in s repeats itself, and false otherwise. For example, if s is "toe-to-toe" and n is 3, then the function would return true since "toe" occurs twice within s. However, if n were 4, the function would return false. As a second example, if s is "singing" and n...
Briefly explain why the head function is not considered “safe” in Haskell functional programming language.
Write a function that takes an int as an argument and returns true if the int is an even number and false if not. In any case change the value of the original argument to twice its value. Use the integers 0 through 4 as test cases. Do not use any arrays.
**Must be done in Haskell** Write the following function: check :: String -> String Which takes in a string with multiple parentheses, brackets and curly brackets and check if they are correct. If they are correct output the string "Correct", and if not output "Wrong" They must only be matched against the same type, ie "({)}" should not be correct. It should not matter that the string contains other letters in it
Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...
Write a function count_vowels(s) that takes a string as an argument and returns the number of vowels ('a', 'e', 'i' 'o', 'u') in the string. Should you use a for or while loop? (Implement this as a function, not as a class with a method.) Be sure to include unittest test cases to demonstrate that your code works properly, e.g Part 2: last_occurance(target, sequence) Write a function last_occurance(target, sequence) that takes two arguments: 1. target: A target item to find...
in python Write a function that takes a string as an argument returns a new string that is that string repeated 3 times.
C++ programming language Write a program that contains following function: A function that receives a string of character, the function return true if the string contains letter c or C, otherwise, return false.