How can I show the output of two print statements from two different functions on the same line? - function

The code for my question is as follows:
def score_Calc(par, strokes, hole_number):
if strokes == par - 1:
print ('On hole #',hole_number, 'a par',par,'you shot a birdie.')
def score_Calcs (par, strokes, hole_number):
if strokes == 1:
print('with a Hole in One!')
My question is this: How can I get the printed results on one line? Right now, I've got it printing out as:
On hole # 5 a par 5 you shot 5 over par.
with a Bo Derek. Bummer.
How can I get it to print out as this?
On hole # 5 a par 5 you shot 5 over par. with a Bo Derek. Bummer.
I realize the code doesn't make much sense, as it is not my full program. My problem is when I execute the main function, it prints out as two seperate lines. How can I make it just one line with the input from the two different functions?

By default, print ends with a newline character. This call
print("Hello World")
is in reality used like that by Python :
print("Hello World", end='\n')
Basically you just need to change the ending :
print ('On hole #',hole_number, 'a par',par,'you shot a birdie.', end=' ')

Related

Need help in understanding why for loop didn't iterate whole list, yet while did

I'm just beginning Python and I wanted to write some functions based on an exercise, and then call these functions from another function, all to extract first letters from each word in a string.
At first I tried a for loop but it didn't iterate through the whole list of words; only halfway. But a while loop did. Yet when I had the for loop just iterate the list and print out each element it worked. I'm struggling to identify why and hope someone can help me understand.
def all_first_letters(sentence):
words = take_words_from_sentence(sentence)
while words != []:
focal1 = first_word_in_list(words)
litreacha = split_word_into_chars(focal1)
litir = first_letter_popped(litreacha)
add_to_list(litir)
This function works and takes a sentence, breaks it into a list of words, pops off the first word, then pops of the first letter of that, appends the letter to a list and then repeats until I end up with a list of initials of each word.
But when I tried it first with a for loop;
def all_first_letters(sentence):
words = take_words_from_sentence(sentence)
for word in words:
focal1 = first_word_in_list(words)
litreacha = split_word_into_chars(focal1)
litir = first_letter_popped(litreacha)
add_to_list(litir)
It would never iterate through the whole sentence. If I gave it a sentence of three words I got two letters; if five I got three letters, and of ten words it returned five.
My puzzle is why the for loop doesn't go through each element of the list words yet the while loop does.
I hope there's enough information here to understand without giving every function.

SSRS chart lines not connecting

I have an SSRS Line chart which plots supply points with square feet on the X axis and Price on the Y axis. Right now I don't really care about making it pretty just getting the lines to show up correctly. I am plotting the points and grouping by Subdivision/Builder.
So for example Subdivision A has builders Y and Z. I want to show different colors and lines for Subdivision A builder Y verses Subdivision A Builder Z.
The problem is that the lines are not connecting when a point for another subdivision builder combination breaks up that line.
The grey line and points below are not all connected as the yellow point is between the grey points so the grey line is not connected to all grey points.
How can I make the points of the same color (same Subdivision/Builder) connected via a line?
As I found out the hard way recently, this problem is often caused by null values in the data not being properly handled by SSRS. Without seeing your data, I can't be certain that's the cause, but nulls were the culprit I encountered the same behavior.
The solutions usually involve assigning values to the color of the EmptyPoint property on the Series, sometimes in conjunction with setting the EmptyPointValue to specify null handling. I've found many references to this problem on the web, but I'll only post links to the best two, both of which are on StackExchange:
The thread SSRS Line Chart NULL VALUE - Horizontal Line contains a thorough discussion of this issue. The usual workaround given is to hard-code a color expression for each line using an IIf, but sometimes this isn't an option, especially if the field you're grouping on has dynamic, unpredictable values, as my dataset did.
The picture posted there depicts clear examples of the same type of line breaks. The user named trubs posted a code sample which illustrates how to set the EmptyPoint, in case where an Iif will work:
=iif(isNothing(Fields!SelectedValue.Value),'No Color',"LightBlue")
The first reply in SSRS Line Chart Not Connecting Data Points details a workaround for cases when the EmptyPoint value & nulls are the root cause and simple hard-coded IIfs won't do the trick. Although I have yet to get my line colors to match the point markers the way I'd like, I can verify that this solution at least gives you your lines back and allows you to assign a variety of colors to them. It's fairly simple and involves merely pasting in some VB code for a couple color properties.
I was asked in the comments section to provide the details of the solutions, but don't want to plagiarize, so I'll simply do a long direct quote of JohnBob's answer:
Firstly, in order to get the lines to join up, you need to set the
EmptyPoint colour for the series.
Select your series in your chart In the properties tab (not the
dialog) drill down into the EmptyPoint property and set the colour to
be Black
This will get them joining up - yay! But part of the line is colour
and the other part is black, right? That's a bit silly, especially
considering if you leave the colour to Automatic on the EmptyPoint
that it will be transparent.
So, then we need to get the series and the EmptyPoint's colours in
sync. Using code from here. I added some code to the code of the
report.
1). Right click on an empty space on the report and select "Report
Properties" 2). In the code tab, paste the following:
Private colorPalette As String() = {"#418CF0", "#FCB441", "#E0400A", "#05642E", "#1A3B69", "#BFBFBF", "#E0400A", "#FCB441", "DarkBlue", "Tomato", "Orange", "CornflowerBlue", "Gold", "Red", "Green", "LightBlue", "Lime", "Maroon", "LightSteelBlue", "Tan", "Silver"}
Private count As Integer = 0
Private mapping As New System.Collections.Hashtable()
Public Function GetColor(ByVal groupingValue As String) As String
If mapping.ContainsKey(groupingValue) Then
Return mapping(groupingValue)
End If
Dim c As String = colorPalette(count Mod colorPalette.Length)
count = count + 1
mapping.Add(groupingValue, c)
Return c
End Function
Then we need to call this code when setting the colour of the series
and of the EmptyPoint.
Select your series
In the properties tab paste something the following (replace WhateverTheGroupIsForYourSeries with your series group name):
=Code.GetColor(Fields!*WhateverTheGroupIsForYourSeries*.Value)
Drill down to the color element of the EmptyPoint Series property
Paste the same text as from point two [e.g. =Code.GetColor(Fields!*WhateverTheGroupIsForYourSeries*.Value)]
And voila! You're done! I can't believe how unnecessarily difficult
this is :D
I hope this helps.
Just put your Fields!(YourSeriesGroup).Value in Series Groups to above of
Fields!(YourCategoryGroup).Value in Category Groups, your series group should be in both Series Groups and Category Groups (should be above of your initial category group).
And after that right click horizontal axis and select Horizontal Axis Properties. Set Axis Type to Scalar and click OK.

Printing a table with sage math

The assignment is to construct a two-column table that starts at x= -4 and ends with x= 5 with one unit increments between consecutive x values. It should have column headings ‘x’ and ‘f(x)’. I can't find anything helpful on html.table(), which is what we're supposed to use.
This what I have so far. I just have no idea what to put into the html.table function.
x = var('x')
f(x) = (5 * x^2) - (9 * x) + 4
html.table()
You might want to have a look at sage's reference documentation page on html.table
It contains the following valuable information :
table(x, header=False)
Print a nested list as a HTML table. Strings of html will be parsed for math inside dollar and double-dollar signs. 2D graphics will be displayed in the cells. Expressions will be latexed.
INPUT:
x – a list of lists (i.e., a list of table rows)
header – a row of headers. If True, then the first row of the table is taken to be the header.
There is also an example for sin (instead of f) with x in 0..3 instead of -4..5, that you can probably adapt pretty easily :
html.table([(x,sin(x)) for x in [0..3]], header = ["$x$", "$\sin(x)$"])
#Cimbali has a great answer. For completeness, I'll point out that you should be able to get this information with
html.table?
or, in fact,
table?
since I would say we want to advocate the more general table function, which has a lot of good potential for you.

Get the most probable color from a words set

Are there any libraries existing or methods that let you to figure out the most probable color for a words set? For example, cucumber, apple, grass, it gives me green color. Did anyone work in that direction before?
If i have to do that, i will try to search images based on the words using google image or others and recognize the most common color of top n results.
That sounds like a pretty reasonable NLP problem and one thats very easy to handle via map-reduce.
Identify a list of words and phrases that you call colors ['blue', 'green', 'red', ...].
Go over a large corpus of sentences, and for the sentences that mention a particular color, for every other word in that sentence, note down (word, color_name) in a file. (Map Step)
Then for each word you have seen in your corpus, aggregate all the colors you have seen for it to get something like {'cucumber': {'green': 300, 'yellow': 34, 'blue': 2}, 'tomato': {'red': 900, 'green': 430'}...} (Reduce Step)
Provided you use a large enough corpus (something like wikipedia), and you figure out how to prune really small counts, rare words, you should be able to make pretty comprehensive and robust dictionary mapping millions of the items to their colors.
Another way to do that is to do a text search in google for combinations of colors and the word in question and take the combination with the highest number of results. Here's a quick Python script for that:
import urllib
import json
import itertools
def google_count(q):
query = urllib.urlencode({'q': q})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
search_response = urllib.urlopen(url)
search_results = search_response.read()
results = json.loads(search_results)
data = results['responseData']
return int(data['cursor']['estimatedResultCount'])
colors = ['yellow', 'orange', 'red', 'purple', 'blue', 'green']
# get a list of google search counts
res = [google_count('"%s grass"' % c) for c in colors]
# pair the results with their corresponding colors
res2 = list(itertools.izip(res, colors))
# get the color with the highest score
print "%s is %s" % ('grass', sorted(res2)[-1][1])
This will print:
grass is green
Daniel's and Xi.lin's answers are very good ideas. Along the same axis, we could combine both with an approach similar to Xilin's but more simple: Query Google Image with the word you want to find the color associated with + a "Color" filter (see in the lower left bar). And see which color yields more results.
I would suggest using a tightly defined set of sources if possible such as Wikipedia and Wordnet.
Here, for example, is Wordnet for "panda":
S: (n) giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca
(large black-and-white herbivorous mammal of bamboo forests of China and Tibet;
in some classifications considered a member of the bear family or of a separate
family Ailuropodidae)
S: (n) lesser panda, red panda, panda, bear cat, cat bear,
Ailurus fulgens (reddish-brown Old World raccoon-like carnivore;
in some classifications considered unrelated to the giant pandas)
Because of the concise, carefully constructed language it is highly likely that any colour words will be important. Here you can see that pandas are both black-and-white and reddish-brown.
If you identify subsections of Wikipedia (e.g. "Botanical Description") this will help to increase the relevance of your results. Also the first image in Wikipedia is very likely to be the best "definitive" one.
But, as with all statistical methods, you will get false positives (and negatives , though these are probably less of a problem).

is line folded? - How to check for folds in VIM

I'm writing some folding functions and I am at a point where I need to check if the current line is actually a fold.
The reason for this is because it is a custom fold method that depends on searching/matching certain lines.
For example, if the current line is folded and looks like:
-FOO------------------------
If you do something like:
getline('.')
You would basically get FOO so there is no way (that I know of) to know if I am at a fold or not.
Is there a helper function for this?
I would think it would have to be something like:
is_folded('.')
I could probably mess with the foldtext to assign a special title for the fold but I want to avoid this.
From :help eval.txt
foldclosed({lnum})
The result is a Number. If the line {lnum} is in a closed
fold, the result is the number of the first line in that fold.
If the line {lnum} is not in a closed fold, -1 is returned.
You can check for a given line if it returns -1 or a line number, you can probably implement your isfolded() function this way.
If you are looking for Vim script function or feature , it is a good idea to start by searching in eval.txt which contains lots of relevant information.