Appearance
question:Given the inputted Python code, find and fix any syntax errors. def square(x): y = x ** return(y)
answer:def square(x): y = x ** 2 return(y)
question:Write a C++ function that calculates and returns the factorial of a given non-negative integer.
answer:#include <iostream> unsigned long long factorial(int n) { if (n <= 1) { return 1; } return n * factorial(n - 1); } int main() { int number; std::cout << "Enter a non-negative integer: "; std::cin >> number; std::cout << "Factorial of " << number << " is: " << factorial(number) << std::endl; return 0; }
question:Using Ruby, implement a method that verifies if a given string is a palindrome after ignoring spaces, punctuation and capitalization.
answer:def palindrome?(str) cleaned_str = str.downcase.gsub(/W/, '') cleaned_str == cleaned_str.reverse end
question:Create a Python function that accepts a string and returns a new string with every character duplicated. For example, if the input string is 'hello', the output should be 'hheelllloo'.
answer:def duplicate_characters(s): result = '' for char in s: result += char * 2 return result input_string = 'hello' output_string = duplicate_characters(input_string) print(output_string)