0

I just finished taking an entry placement test for computer science as in college. I passed, but missed a bunch of questions in a specific category: variable assignment. I want to make sure I understand this before moving on.

It started out with easy things, like "set age equal to age"

int age = 18, pretty simple

But then, it had a question which I had no clue how to approach. It went something like...

"Determine if character c is is in alphabet and assign to a variable"

I could easily do that with a function, but the issue is, it gave me literally a line to write my entire answer (so about 50 characters max). Here is how the answer box looked:

enter image description here

My first thought was to do something like

in_alphabet = function(c) {
  var alphabet = ["a", "b" ... "z"]
  if(alphabet.indexOf(c) != -1)
      return true;
}

But this solution has two issues:

  1. How can I set the "c" value when the whole function is equal to in_alphabet?
  2. I can't fit this into the small answer box. I am 99% sure they were looking for something else. Does anybody know what they were looking for? I can't think of a one line solution for this

Language doesn't matter (although a solution in java/c++ would be preferred). I would appreciate any guidance (doesn't have to be a solution, I just don't even know where to begin)

Shivang Saxena
  • 195
  • 1
  • 2
  • 13

5 Answers5

0

I copied straight from How to check if character is a letter in Javascript?

in_alphabet = c.length === 1 && c.match(/[a-z]/i)? str : ""
Community
  • 1
  • 1
rassa45
  • 3,482
  • 1
  • 29
  • 43
0

The question "Determine if character c is is in alphabet and assign to a variable" does not ask you to create a function (although in many languages this would be the best way to do this).

In R you could do something like:

inAlphabet <- c %in% letters

So you can certainly do it in one line in some real-world languages. Note that letters is a built-in list of characters.

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
0

It's a VBA solution and returns C in the variable:

LetterC = Mid("ABCDEFGHIJKLMNOPQRSTUVWXYZ", InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "C"), 1)

Is that what you're after?

Darren Bartrup-Cook
  • 18,362
  • 1
  • 23
  • 45
0

Many languages have a data type that represents a single character, and they often can be compared using binary operators like < > <= >=, wherein the characters are compared numerically.

So something like this should suffice:

in_alphabet = c >= 'a' && c <= 'z'

And some languages already have built in methods to do things similar to this (e.g., Character.isLetter).

sgbj
  • 2,264
  • 17
  • 14
0

In Java, Character.isLetter(c)
In .NET, Char.IsLetter(c)

Perhaps you were being tested on knowledge of basic data types and some of the facilities they provide.

Jay
  • 56,361
  • 10
  • 99
  • 123