want someone to do my homework for me [closed] - python-3.x

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
# Change this program to output each letter of the text entered on a separate line
# with space in front of each letter, increasing to the right for each letter.
# For example, 'Python' input should output:
# P
# y
# t
# h
# o
# n
text = input('Enter text: ')
for letter in text:
print(letter)
I already tried to look online for the solution, there are none.
This code is for homework but i cant figure it out help wold be appreciated

As you haven't told us what you have tried to do so far, or what you have tried to learn to figure it out, I'm not going to post the Python code. Instead, lets think about what the program should be doing. Your assignment statement gives a broad overview, but as a programmer you need to take this overview and turn it into a set of smaller instructions. These smaller steps do not have to be in code. They can be in whatever form you like, even plain english.
For functional analysis (which is what you are doing for this problem) start with the inputs and outputs then fill in the stuff in-between.
1) Input: a string
X) Output: multiple lines with a single character and whitespace
Now how do you want to get from 1 to X. You already have the code to loop through each letter and print it, but you are missing two things to get to your required output.
A) way to place a string on a new line
B) way to add whitespace to a string
I'll give you a couple of hints. A) is something that is extremely common in almost any programming language. In fact, it is done the exact same way is any language that you are likely to use. Another hint. Your final output will be a single string that spans multiple lines. Ask yourself how a word processor or text editor gives works with blank lines.
B is a little more tricky, as there are a couple of nifty Python tricks that makes it simpler to do than in other languages. I'm guessing that you already know what happens when you add two numbers together such as 3 + 5. But what happens when you add two strings together such as "foo" + "bar". Notice how the behavior for adding numbers with the + operator is completely different than the behavior is for adding strings together with the same operator? That difference in behavior applies to other common operators as well. Play around with the other three common mathematical operators on both string and numbers to see what happens

Related

python beginner : Dynamically modifying a python list with indexing/slicing to preform arithmetic operators [duplicate]

This question already has answers here:
Sum a list of numbers in Python
(26 answers)
Closed last month.
I have been stuck trying to write code that will dynamically take user input from a list and preform general arithmetic operators. In order to work around this I used indexing and slicing which did solve my problem temporarily but a new problem rose from doing this.
listgrades= []
num_students = int(input("How many students are you evaluating?"))
def student_info():
for i in range(0, num_students):
student_name=input("Enter your name here: ")
studnet_age=input("Enter your age here: ")
student_total_grade=int(float(input("What is your total grade")))
listgrades.append(student_total_grade)
student_info()
grades_sum= (listgrades[0] + listgrades[1] + listgrades[2]) / num_students
print(f"The average of all the student grades is {grades_sum}")
`
I'm trying to change the (listgrades[0] + listgrades[1] + listgrades[2]) to something more changeable, workable and scalable
I was trying to look and find a solution or a way to work around this but I hit a dead end and I ran out of ideas at this point.
I think a loop of some sorts might work for this but I'm not sure.
side note: I kinda looked into numpy and I can't use it since my school lab computers won't allow anything out of the default python module library.
I have some general advice and some specific suggestions.
For general advice, to see what built in methods are available is to use python itself.
At the command line type python3
then, within python type dir(list)
and you will see the available methods for lists.
You get more detailed information about any specific method by typing help and the class you are interested in followed by a dot, than the method name. For example help(list.count).
You can also type help(list) to get a more in depth list of all the functions and instructions for use. Type space to continue to the next screen, and b to backup a screen. Type q to end the help screens.
To exit python type exit()
More specifically, I agree that a loop would be a more dynamic direction to go, given you are asking how many students to evaluate in your input.
One way to loop through your list would be:
for x in listgrades:
sum = sum + x
Of course, you can perform other math operations inside the loop, or in similar loops. Presumably you will initialize your value before the loop.
At some point you may need to count how many grades are in your list. Fortunately, there's a built-in method for lists that gives you that information called count. You'll see this if you use python's dir(list) or help(list).
number_of_grades = listgrades.count
I think this will point you in the direction you were thinking with the code, without giving away much in exactly how, which is what you are learning. Best of luck!

Diff between constant and variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
What exactly is the difference between a constant and a variable. Can constants be understood as values that can be assigned to variables in a program
You basically have the right idea. The correct idea is a bit hard to give with the small amount of information you give in your question.
"constant" is a bit vague. The name is used to refer to literals, symbolic constants, constant expressions, immutable variables ...
Anyway, the answer depends strongly on what language you're using and what context you've heard the term "constant" in.
For example, in the C programming language, most symbolic constants do not exist at runtime. They are simply names that are replaced by their actual literal values as the first step before compilation.
In other languages, constants are named variables that are saved into the built program that contain a value that can't be changed, and can be listed or so.
Wait, constants are sometimes variables?
Well, the terms "constant" and "variable" are kind of vague concepts that are sometimes used wrongly, and don't have a straight translation into machine code.
At the core, there is just memory. And memory contains data. Constants are usually parts of the memory that are just loaded from disk for you by the operating system together with your compiled code, and then your code can read them. Variables are parts of the memory for which "gaps" in memory are set aside by the system, and then your code can put values into it or read it.
That's why it's a bit hard to offer a concise definition of a variable and a constant. It depends on what level of the computer you are looking at it, through which language.
In most languages, a symbolic constant is simply a more convenient name you can use in your code to refer to a fixed number or other literal value. A name whose value you can change in one central location before you compile your code, and all other places that use the symbolic name automatically pick up the value.
Variables are boxes into which you can put any value.
So you're basically right. But there can be more to the story depending on what language you're using.
The reason for symbolic constants is mostly to make your code more readable. Instead of
leftCoordinate = 16 + 20 + 4
you can write
leftCoordinate = LEFT_MARGIN + SIDEBAR_WIDTH + LINE_WIDTH
and suddenly it is much more obvious which of these numbers you have to change to change the right part. Also, you can use them to make sure two numbers always match. Like, elsewhere in your program, you may have the code that draws the "line" mentioned above, and just do
setLineWidth(LINE_WIDTH)
drawLine(LEFT_MARGIN + SIDEBAR_WIDTH, 0, LEFT_MARGIN + SIDEBAR_WIDTH, 100)
And if you ever decide you want a thinner line, you just change the constant's value, and all your code magically updates, and you just need to recompile.

Why an "i" in "for i in range"?

I'm new to programming and Python is my first language of choice to learn. I think it's generally very easy and logical and maybe that's why this minor understanding-issue is driving me nuts...
Why is "i" often used in learning material when illustrating the range function?
Using a random number just seems more logical to me when the range function is dealing with numbers..
Please release me from my pain.
A little extra help!
Asking why we use i is kinda like asking why so many people user the letter x in math problems. It is mostly because i is just a very easy variable to use to represent the current increment in a loop.
I also think you are confused about the place of i in a loop. When you use a range loop you are saying that you want to count one by one from one number until you hit another. Typically it would look like this
for i in range(0, 5):
This means I want to count from 0-4 and set ito the current loop I am currently on.
A great way to test this.
for i in range(0, 5):
print("i currently equals: ", i)
The result will be
i currently equals: 0
i currently equals: 1
i currently equals: 2
i currently equals: 3
i currently equals: 4
In your question you ask why don't you set i to a number and it is because you can not use numbers as variable names. Python can not accept what you are asking, but if it could it would look like this
for 54 in range(0, 5):
print(54)
Try reading up a little more on what variables are and how to properly use them in programming.
https://en.wikibooks.org/wiki/Think_Python/Variables,_expressions_and_statements
Lastly good luck in your pursuit to become a programmer! Coding is one of the most exciting things in this world to many of us and I hope one day you will feel the same!
i is used across nearly all programming languages to indicate a counting variable for a iteration loop.
Answered here.
i and j have typically been used as subscripts in quite a bit of math for quite some time (e.g., even in papers that predate higher-level languages, you frequently see things like "Xi,j", especially in things like a summation).
When they designed Fortran, they (apparently) decided to allow the same, so all variables starting with "I" through "N" default to integer, and all others to real (floating point). For those who've missed it, this is the source of the old joke "God is real (unless declared integer)".
Most people seem to have seen little reason to change that. It's widely known and understood, and quite succinct. Every once in a while you see something written by some psychotic who thinks there's a real advantage to something like:
for (int outer_index_variable=0; outer_index_variable < 10; outer_index_variable++) for (int inner_index_variable=0; inner_index_variable < 10; inner_index_variable++) x[outer_index_variable][inner_index_variable] = 0;
Thankfully this is pretty rare though, and most style guides now point out that while long, descriptive variable names canbe useful, you don't always need them, especially for something like this where the variable's scope is only a line or two of code.

How do I find a version number in a string using perl [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to write a program that searches a string for a version number.
The length of the version may differ and is like this
[number].[number].[....].[number]
for example 1.23.1.
I found something similar in How to extract IP addresses from a text file using Perl?
You should study up on some simple regex expressions, because this isn't that hard of a problem. The regular expression you're searching for might be this:
(\d+\.)+\d+
This will find a string like 1.23.235.22.263
You're supposed to show some effort at solving the problem yourself. But, hey, it's Friday and I'm feeling generous.
[number].[number].[....].[number](e.g. 1.23.1).
This is enough to get us going. By "number" here, I assume that you mean "positive integer". A positive integer is one or more digits. And in regular expressions a digit is matched with \d and we can match "one or more" of something with a +. So "one or more digits" is d+.
So what's our minimal version number. Let's assume it contains two positive integers separated by a dot. We need to be careful here as a dot has a special meaning in regexes (it matches any character). We can turn it into just a dot by escaping it with \. So our minimal version number matches \d+\.\d+.
Now it gets a bit harder. We want to allow any number of \d+\. elements in the middle of our string. A naïve version might be \d+\.(\d+\.)*\d+. The * means "zero or more", so the whole expression means "One or more digits followed by a dot, followed by zero or more repetitions of one or more digits followed by a dot, followed by one or more digits".
But we can improve on that. "One of something" followed by "zero or more of the same thing" is the same as saying "one of more of something". So we can simplify our regex to (\d+\.)+\d+.
Also, as we're going to be capturing the matches, I'd convert that (...) to (?:...) which still groups the regex parts, but doesn't capture the matches.
So we end up with (?:\d+\.)+\d+. Given a string in $string, we can get populate an array with all of the version numbers contained in $string using:
my #versions = $string =~ /((?:\d+\.)+\d+)/g;

How do I group similar strings in R? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I have a database with ~5,000 locality names, most of which are repetitions with typos, permutations, abreviations, etc. I would like to group them by similarity, to speed up further processing. The best would be to convert each variation into a "platonic form", and put two columns side by side, with the original and platonic forms. I've read about Multiple sequence alignment, but this seems to be mostly used in bioinformatics, for sequences of DNA/RNA/Peptides. I'm not sure it will work well with names of places. Anyone knows of a library that helps me to do it in R? Or which of the many algorithm variations might be easier to adapt?
EDIT: How do I do that in R? Up to now, I'm using adist() function, which gave me a matrix of distances between each pair of strings (although it don't treat translocations the way I think it should, see comment below). The next step I'm working right now is to turn this matrix into a grouping/clustering of similar enough values. Thanks in advance!
EDIT: To solve the translocations problem, I did a small function that gets all the words with more than 2 characters, sort them, removes any punctuation left, and paste them again into a string.
sep <- function(linha) {
resp <- strsplit(linha," |/|-")
resp <- unlist(resp)
resp <- gsub(",|;|\\.","",resp)
resp <- sort(resp[which(nchar(resp) > 2)])
paste0(resp,collapse=" ")
}
Then I apply this over all lines of my table
locs[,9] <- apply(locs,1,function(x) sep(x[1])) # 1=original data; 9=new data
and finally apply adist() to create the similarity table.
There's a built in function called "adist" that computes a measure of distance between two words.
It's like using "agrep", except it returns the distance, instead of whether the words match according to some approximate matching criteria.
For the special case of words that can be interchanged with a comma(e.g. "hello,world" should be close to "world,hello"), here's a quick hack. You can modify the function pretty easily if you have other special cases.
adist_special <- function(word1, word2){
min(adist(word1, word2),
adist(word1, gsub(word2,
pattern = "(.*),(.*)",
repl="\\2,\\1")))
}
adist("hello,world", "world,hello")
# 8
adist_special("hello,world", "world,hello")
# 0

Resources