A) Write a function named isWhiteSpace() that accepts a character and returns tr
ID: 3842399 • Letter: A
Question
A) Write a function named isWhiteSpace() that accepts a character and returns true if it is a white space, false otherwise. Use a RegExp.
Hint: s - Matches a single white space character, including space, tab, form feed, line feed.
D) Write a function named isPalindrome, that accepts a sentence containing blanks and punctuation and returns true if it’s a palindrome, false otherwise. HINT: reverseStr from P3 has a role to play here.
E) Write a function named factorial() that accepts a positive integer N and returns N!
F) Write a function nChooseK(), that accepts two positive integers, N and K, and returns the number of ways to arrange K items out of a set of N. You should use factorial() in this function.
G) Write a function factorsOfN(), that accepts a positive integer N, and returns an array containing all of its factors.
H) Write a function isPrime(), that accepts a positive integer and returns true if it is a prime, false otherwise. You should use factorsOfN() in this function.
Explanation / Answer
I have written these functions considering JAVA language:
(A)
boolean whitespaceSearchRegExp(String input) {
return java.util.regex.Pattern.compile("\s").matcher(input).find();
}
(D)
boolean isPalindrome(String s) {
int n = s.length();
for (int i = 0; i < (n/2); ++i) {
if (s.charAt(i) != s.charAt(n - i - 1)) {
return false;
}
}
return true;
}
(E)
int factorial(int number){
int i,fact=1;
for(i=1;i<=number;i++){
fact=fact*i;
}
return fact;
}
(F)
int nChooseK(int n, int k)
{
int number1 = factorial(n);
int number2 = factorial(n-k);
int number3 = factorial(k);
return number1 / (number2 * number3);
}
(G)
ArrayList<Integer> factorsOfN(int num){
ArrayList<Integer> aux=new ArrayList<>();
for(int i=1;i<=num/2;i++){
if((num%i)==0){
aux.add(i);
}
}
return aux;
}
(H)
boolean isPrime(int num){
ArrayList<Integer> factors = factorsOfN(num);
if(factors.size() == 1)
return true;
else
return false;
}
I am writing the solution considering JavaScript :
(A)
<script>
var str = "ABC DEF";
var pat2 = new RegExp("\s");
document.writeln(pat2.test(str));
</script>
(D)
<script>
function checkPalindrome(str) {
return str == str.split('').reverse().join('');
}
str = checkPalindrome("NI:;;:IN")
document.write(str)
</script>
(E)
<script>
function Fact(num)
{
if (num === 0)
{ return 1; }
else
{ return num * Fact( num - 1 ); }
}
fact = Fact(5)
document.write(fact)
</script>
(F)
<script>
function nChooseK(n, k)
{
number1 = Fact(n);
number2 = Fact(n-k);
number3 = Fact(k);
return number1 / (number2 * number3);
}
function Fact(num)
{
if (num === 0)
{ return 1; }
else
{ return num * Fact( num - 1 );
}
}
fact = nChooseK(8,3)
document.write(fact)
</script>
(G)
<script>
function getFactors(integer){
var factors = [],
quotient = 0;
for(var i = 1; i < integer; i++){
quotient = integer/i;
if(quotient === Math.floor(quotient)){
factors.push(i);
}
}
return factors;
}
fact = getFactors(10)
document.write(fact)
</script>
(H)
<script>
function isPrime(number)
{
factors = getFactors(number)
if (factors.length == 1)
return true;
else
return false;
}
function getFactors(integer){
var factors = [],
quotient = 0;
for(var i = 1; i < integer; i++){
quotient = integer/i;
if(quotient === Math.floor(quotient)){
factors.push(i);
}
}
return factors;
}
fact = isPrime(29)
document.write(fact)
</script>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.