Simple Symbols Coderbyte Solution

0

Have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence by either returning the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter.

Simple Symbols Program in Java


import java.util.Scanner;


public class SimpleSymbols {
final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
final String ALPHABET_UPPER = ALPHABET.toUpperCase();

String SimpleSymbols(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (ALPHABET.indexOf(c) > -1 || ALPHABET_UPPER.indexOf(c) > -1) {
if (i == 0 || i == str.length() - 1) {
return "false";
}

char cp = str.charAt(i - 1);
char cn = str.charAt(i + 1);

if (cp != '+' || cn != '+') {
return "false";
}
}
}


return "true";

}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
SimpleSymbols c = new SimpleSymbols();
System.out.print(c.SimpleSymbols(s.nextLine()));
}
}

Simple Symbols Coderbyte python


def SimpleSymbols(str):

# import re to remove certain characters from string
import re

# create alphabet list
alphabet = list('abcdefghijklmnopqrstuvwxyz')
# convert string to lower
str = str.lower()
# set n = 0 for loop
n = 0

# create new var letters based on number of letters in string
letter_str = re.sub("[^abcdefghijklmnopqrstuvwxyz]", "", str)
letters = len(letter_str)

# if first or last element in the string is in the alphabet then return False
if str[0] in alphabet or str[-1] in alphabet:
return False

# otherwise, loop from second element to second to last element
else:
for x in range(1,len(str)-1):

# get letter
index = alphabet[x]

# if letter in alphabet and the character before and after is a "+"
# then add one to n, otherwise, dont add anything
if index in alphabet and str[x-1] == "+" and str[x+1] == "+":
n += 1
else:
n += 0

# if n is equal to number of letters in string, then each letter in the string
# has a "+" both before and after the letter
if n == letters:
return True
else:
return False
print(SimpleSymbols(input()))

Simple Symbols JavaScript

Using the JavaScript language, have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence by either returning the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter.

Simple Symbols JavaScript


function SimpleSymbols(str) {

var arr = str.toLowerCase().split("");
for (var i = 0; i < arr.length; i++) {
if (arr[i] >= "a" && arr[i] <= "z") {
if (i === 0 || i === arr.length) {
return false;
}
if (arr[i-1] !== "+" || arr[i+1] !== "+") {
return false;
}
}
}
return true;
}

Explanation

When reading the directions for this challenge you have to be aware of the adage "Attention to detail, all the time!" The instructions immediately tell you that you will have two outliers that you have to account for in your solution. They are the first and last characters. If these are letters then you have to return false because there cannot be a + before or after these letters.

Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !