How to define a function involving strings?

In summary: Then you just need to determine if the rule is one-to-one, onto, or bijective. A function f:X->Y is just a rule that gives you a unique element of Y for each element of X. In this case, the rule is simple: drop the last digit.
  • #1
DevNeil
4
0

Homework Statement



We have a problem set on my Discrete Mathematics class:

Let X be the set of strings over {a,b} of length 4 and let Y be the set of the strings over {a,b} of length 3. Define function f from X to Y by the rule:

f(alpha) = string consisting of the first three characters of alpha.

Is f one-to-one? is f onto?

The Attempt at a Solution


I don't know where to start but what I understand on the problem is that X is a set with {aaaa,bbbb,aaab...etc} and Y has {aaa,bbb,aab...etc}. Do I need to list all the elements of the sets? I was also confused by the alpha variable. Hoping for your answers. Thanks.
 
Last edited:
Physics news on Phys.org
  • #2
Alpha is any element in the set X, so any string of length 4. Your function f takes an element of X and maps it into an element of Y. For example, f(bbaa) = bba.

What does it mean for f to be one-to-one or onto?
 
  • #3
clamtrox said:
Alpha is any element in the set X, so any string of length 4. Your function f takes an element of X and maps it into an element of Y. For example, f(bbaa) = bba.

What does it mean for f to be one-to-one or onto?

You need to state if it's injective(1to1) or surjective(onto) or bijective(injective && surjective). I can say that this is not one-to-one (injective) because as counterexample f(aabb) = aab , f(aaba)= aab, same result but different alpha.

Can you also tell me how to define the function formally?
 
  • #4
DevNeil said:
You need to state if it's injective(1to1) or surjective(onto) or bijective(injective && surjective). I can say that this is not one-to-one (injective) because as counterexample f(aabb) = aab , f(aaba)= aab, same result but different alpha.

And then you only need to figure out if it's onto. That should be straightforward too.

DevNeil said:
Can you also tell me how to define the function formally?
The definition you were given looks pretty formal to me. Is there something wrong with it?
 
  • #5
clamtrox said:
And then you only need to figure out if it's onto. That should be straightforward too.


The definition you were given looks pretty formal to me. Is there something wrong with it?

Ohh... I am thinking of a function that was mathematically defined. So another question, is this an acceptable definition of a function? I am having a hard time defining it. Thanks for your time :)
 
  • #6
DevNeil said:
Ohh... I am thinking of a function that was mathematically defined. So another question, is this an acceptable definition of a function? I am having a hard time defining it. Thanks for your time :)

A function f:X->Y is just a rule that gives you a unique element of Y for each element of X. In this case, the rule is simple: drop the last digit.
 
  • #7
You could just list all 16 ordered pairs (x,f(x)) in X×Y.
 

What is a function involving strings?

A function involving strings is a block of code that takes in one or more strings as inputs, performs a specific task on them, and returns a result. It can be used to manipulate, analyze, or format strings in various ways.

How do I define a function involving strings?

To define a function involving strings, you need to use the def keyword, followed by the function name and parentheses containing the string parameters. Inside the function, you can use string operations and methods to perform the desired task on the input strings. Finally, use the return keyword to return the result.

What are some examples of string functions?

Some examples of string functions include len() which returns the length of a string, upper() which converts a string to uppercase, lower() which converts a string to lowercase, and split() which splits a string into a list of substrings based on a delimiter.

How do I pass strings as arguments to a function?

To pass strings as arguments to a function, you simply need to include them inside the parentheses when calling the function. For example, if you have a function named format_name() that takes in two string parameters first_name and last_name, you would call it like this: format_name("John", "Smith").

Can a function involving strings return something other than a string?

Yes, a function involving strings can return any data type, such as integers, booleans, or lists. This is because the return value of a function is not limited to the data types of its input parameters. However, if the function is specifically designed to manipulate strings, it is best to return a string as the result.

Similar threads

  • Precalculus Mathematics Homework Help
Replies
15
Views
636
  • Precalculus Mathematics Homework Help
Replies
3
Views
878
  • Precalculus Mathematics Homework Help
Replies
7
Views
395
  • Precalculus Mathematics Homework Help
Replies
12
Views
483
  • Precalculus Mathematics Homework Help
Replies
13
Views
303
  • Precalculus Mathematics Homework Help
Replies
4
Views
2K
  • Precalculus Mathematics Homework Help
Replies
3
Views
1K
  • Precalculus Mathematics Homework Help
Replies
21
Views
3K
  • Calculus and Beyond Homework Help
Replies
14
Views
1K
  • Precalculus Mathematics Homework Help
Replies
11
Views
515
Back
Top