python for loop getting letters in a word by using return - python-3.6

I'm trying to make a function which will operate like this:
input:
make_nametag('apple')
output:
'a*p*p*l*e*'
this is my code:
def make_nametag(name):
for letter in name:
print(letter + '*', end='')
Are there any ways that I can use return instead? Thanks!
Thanks !

I actually figured this out by using:
def make_nametag(name):
get = int(len(name))
a = ''
for letter in name:
a+=str(letter + "*")
return a
if there are any better solutions, please share.

Related

add keys together in a dictionary

Hei guys! I need help in a python program. I wanna make a method which returns the sum of the keys as a dictionary. But I get a error "object is not iterable".
def totaltAntallSalg (dic) :
s = sum (dic.keys)
return s
call_function = totaltAntallSalg({"Ahmed":2,"Nada":1, "hala":3 })
How can I solve this problem?
thanks in advance
How can you add strings ? It might be values that you want to add.
To add values you may use following code:-
def totaltAntallSalg(dic):
D={}
D['sum']=sum(dic.values())
return D

How to make an if statement point free

I have the following if statement:
var formatCity =
obj => R.both(has('city'), has('state'))(obj) ? appendCommaToCity(obj) : obj
I would like to make this code point free, but can not figure out a way around the if statement.
That's quite simple actually using the ifElse function - or its specialisation, when:
const formatCity = R.when(R.both(has('city'), has('state')), appendCommaToCity);

Groovy Map Issue with Variable Properties and String INterpolation

I have been navigating map structures fine for a long time now. Yet, for some reason, the root of this problem escapes me. I've tried bracket notation as well, no luck.
Why doesn't the final output (null) return "[serverinfo:[listenPort:19001]]"
If I replace the two instances of ' "$instanceName" ' with simply ' services ', it works.
String instanceName = "Services"
Map serverNode = [
instances:[
"$instanceName":[
serverinfo:[
listenPort:19001
]
]
]
]
println "$instanceName"
println serverNode.instances
println serverNode.instances."$instanceName"
//output
Services
[Services:[serverinfo:[listenPort:19001]]]
null
The type of "$instanceName" is GStringImpl, not String. It's a common mistake (and hard to find!)
def serverNode = [
instances:[
("$instanceName" as String):[
serverinfo:[
listenPort:19001
]
]
]
]
as stated by #tim_yates in comment, if your interpolated string is as simple as in this example (ie ,"${property}"), then you can use the (property) syntax : Groovy put the value of the property as a key, not the word "property"

How to get all results with cts:search?

I am using cts:search like this:-
let $query :=
cts:or-query((
cts:field-word-query(
"Assignor Name", $assignorName
),
cts:field-word-query(
"Assignee Name", $assigneeName
)
))
for $patent in cts:search(fn:doc(), $query)[1 to 10]
return $patent
where $assignorName and $assigneeName is the input from the user. But when both $assignorName & $assigneeName are empty strings then it does not show any results. I want to show all the results when the user does not enter any input. How can I achieve this ?
More explicitly, you might write the query something like this:
cts:or-query((
if (not($assignorName)) then () else cts:field-word-query(
"Assignor Name", $assignorName),
if (not($assigneeName)) then () else cts:field-word-query(
"Assignee Name", $assigneeName) ))
It takes a while to get used to the idea that you can put an if-then-else expression practically anywhere. Sometimes it helps to think of it as ternary logic.
To get all results, you have to replace the current $query with cts:and-query(()). A cts:or-query(()) might work too by the way.
HTH!

xQuery substring problem

I now have a full path for a file as a string like:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"
However, now I need to take out only the folder path, so it will be the above string without the last back slash content like:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/"
But it seems that the substring() function in xQuery only has substring(string,start,len) or substring(string,start), I am trying to figure out a way to specify the last occurence of the backslash, but no luck.
Could experts help? Thanks!
Try out the tokenize() function (for splitting a string into its component parts) and then re-assembling it, using everything but the last part.
let $full-path := "/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
$segments := tokenize($full-path,"/")[position() ne last()]
return
concat(string-join($segments,'/'),'/')
For more details on these functions, check out their reference pages:
fn:tokenize()
fn:string-join()
fn:replace can do the job with a regular expression:
replace("/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
"[^/]+$",
"")
This can be done even with a single XPath 2.0 (subset of XQuery) expression:
substring($fullPath,
1,
string-length($fullPath) - string-length(tokenize($fullPath, '/')[last()])
)
where $fullPath should be substituted with the actual string, such as:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"
The following code tokenizes, removes the last token, replaces it with an empty string, and joins back.
string-join(
(
tokenize(
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
"/"
)[position() ne last()],
""
),
"/"
)
It seems to return the desired result on try.zorba-xquery.com. Does this help?

Resources