How can I implement the java hashmap by using PHP? - hashmap

I use PHP to process following input:
sam
99912222
tom
11122222
harry
12299933
sam
edward
harry
the 1st to 6th line are name and phone numbe. And the last three lines is the search query, if the name is not in the list(not have phone number,print not found), otherwise output the data. My code as follow:
<?php
$_fp = fopen("php://stdin", "r");
$list = array();
for($i = 0;$i<3;$i++){
$name = strtolower(fgets($_fp));
$phone = fgets($_fp);
$list["$name"] = $phone;
}
for($i = 0;$i<3;$i++){
$name = fgets($_fp);
if(array_key_exists($name,$list)){
echo "$name".'='."$list[$name]"."\n";
}else{
echo 'Not found'."\n";
}
?>
Excepted output should be sam = 99912222 Not found harry = 12299933
The output is sam = 99912222 Not found Not found. why these function doesn't work?
This is a problem from hackerrank.
I know if I use hashmap in java is easy to solve. But how can I solve this problem in PHP?
Many thanks

First, trim off whitespace by using trim(fgets($_fp)) everywhere instead of just fgets($_fp) -- that fixes things on my end at least.
Second, the code you pasted is missing the closing curly bracket on your second for loop.
Third, have fun with 30 Days of Code :-) (once you get the above straightened out you also need to have your code read in the number of entries at the beginning, and "Read the queries until end-of-file" at the end).

Related

Creating a powershell function that will take on ONE parameter and outputs a concatenated string value (fname+lname)

I'm looking for some guidance or tips for how to revise the function below. My goal is create a function that accepts one parameter and then returns a string. In this case the string will be a name of a super hero. The catch is that the function must output the full name concatenated together (ex. $FullName = $FName + "" +$LName). Can anyone give me some guidance on how I can accomplish this?
So far a have functional function that accepts one parameter but the output is the full name as one singular string. I am unable to find away to concatenate a first and last name and use it as my single parameter for this function. The commented out section of the function is just some trial and error code that I have attempted so far. Any help is appreciated!
The strings I'm working with:
Bat Man,
Wonder Woman,
Aqua Man
function Get-DCHero {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)][ValidateSet('Batman','Superman','Aquaman','Wonder Woman',ErrorMessage = "'{0}' is not a DC Super Hero. Please trying one of the following: '{1}'")]
[string]$Name
<#
[Parameter(Mandatory=$true)][ValidateSet('Batman','Superman','Aquaman','Wonder Woman',ErrorMessage = "'{0}' is not a DC Super Hero. Please trying one of the following: '{1}'")]
[string]$FullName = $FName + "" +$LName
[ValidateSet('Bat','Super','Aqua','Wonder','Flash',ErrorMessage = "'{0}' is not a DC Super Hero. Please trying one of the following: '{1}'")]
[string]$FName
[ValidateSet('Man','Man','Man','Woman','Flash',ErrorMessage = "'{0}' is not a DC Super Hero. Please trying one of the following: '{1}'")]
[string]$LName
#>
)
Write-OutPut "$Name is a DC Super hero."
}

Replacing a certain part of string with a pre-specified Value

I am fairly new to Puppet and Ruby. Most likely this question has been asked before but I am not able to find any relevant information.
In my puppet code I will have a string variable retrieved from the fact hostname.
$n="$facts['hostname'].ex-ample.com"
I am expecting to get the values like these
DEV-123456-02B.ex-ample.com,
SCC-123456-02A.ex-ample.com,
DEV-123456-03B.ex-ample.com,
SCC-999999-04A.ex-ample.com
I want to perform the following action. Change the string to lowercase and then replace the
-02, -03 or -04 to -01.
So my output would be like
dev-123456-01b.ex-ample.com,
scc-123456-01a.ex-ample.com,
dev-123456-01b.ex-ample.com,
scc-999999-01a.ex-ample.com
I figured I would need to use .downcase on $n to make everything lowercase. But I am not sure how to replace the digits. I was thinking of .gsub or split but not sure how. I would prefer to make this happen in a oneline code.
If you really want a one-liner, you could run this against each string:
str
.downcase
.split('-')
.map
.with_index { |substr, i| i == 2 ? substr.gsub(/0[0-9]/, '01') : substr }
.join('-')
Without knowing what format your input list is taking, I'm not sure how to advise on how to iterate through it, but maybe you have that covered already. Hope it helps.
Note that Puppet and Ruby are entirely different languages and the other answers are for Ruby and won't work in Puppet.
What you need is:
$h = downcase(regsubst($facts['hostname'], '..(.)$', '01\1'))
$n = "${h}.ex-ample.com"
notice($n)
Note:
The downcase and regsubst functions come from stdlib.
I do a regex search and replace using the regsubst function and replace ..(.)$ - 2 characters followed by another one that I capture at the end of the string and replace that with 01 and the captured string.
All of that is then downcased.
If the -01--04 part is always on the same string index you could use that to replace the content.
original = 'DEV-123456-02B.ex-ample.com'
# 11 -^
string = original.downcase # creates a new downcased string
string[11, 2] = '01' # replace from index 11, 2 characters
string #=> "dev-123456-01b.ex-ample.com"

How to skip a Parameter with Default Values in Groovy?

My Groovy method has 3 parameters and the last 2 have default values. I want to skip the second parameter, and only provide values for the first and the third like so..
def askForADate(girlsName, msg = 'Will you go out with me?', beg = 'pretty please!!') {
println "$girlsName, $msg $beg!"
}
askForADate('Jennifer',,'Because I love you!')
Right now this prints out...
Jennifer, Because I love you! pretty please!!!
So it looks like it is plugging the value I am passing in for the third parameter into the second.
How to fix that?
As doelleri said, you'll need to write two version of thie method.
Unless you'll use some groovy goodness with named arguments!
def askForADate(Map op, girlsName) {
println "$girlsName, ${op.get('msg', 'Will you go out with me?')} ${op.get('beg', 'pretty please!!')}!"
}
askForADate(beg: 'Because I love you!', 'Jennifer')
Prints out: Jennifer, Will you go out with me? Because I love you!!
See http://mrhaki.blogspot.com/2015/09/groovy-goodness-turn-method-parameters.html for more details
This solution has the clear disadvantage of reordering the arguments as now the girls name is last in line.

Split string in multiples lines swift

I'm trying to split a string in multiple lines (such as the text of a song); I'm working in the localizable.strings.
I'd like to do something like this:
"TITLE_SONG01" = "It's my life";
"SUBTITLE_SONG01" = "";
"TEXT_SONG01" = "It's my life" +
"is now or never";
but it doesn't work because data isn't in the correct format. Can anyone help me?
You can do that by inserting "\n" where you want the new line to start. For example:
print("Hello\nWorld")
Which will output the Hello on one line and World on another like this:
Hello
World

How can a text file be automated converted like this using tools in linux?

I have a text file with several lines.
I want to do the following:
1. Remove the first 14 characters. Leave the next 8 characters. Then delete everything on the line after that.
Then the file looks something like this.
20050013
AC040020
AC050024
At the beginning of each line, I want to add something like RAM[n] = 'h, where n keeps incrementing. I also want to add a ; at the end.
Then the file looks like this
RAM[0] = 'h20050013;
RAM[1] = 'hAC040020;
RAM[2] = 'hAC050024;
There has to be 10 entries in the file. So I add remaining entries and set them to 0. The file ends up like this:
RAM[0] = 'h20050013;
RAM[1] = 'hAC040020;
RAM[2] = 'hAC050024;
RAM[3] = 'h00000000;
RAM[4] = 'h00000000;
RAM[5] = 'h00000000;
RAM[6] = 'h00000000;
RAM[7] = 'h00000000;
RAM[8] = 'h00000000;
RAM[9] = 'h00000000;
I guess I could use a perl script or vi. How can this process be automated?
Have you read man sed?
Did you try doing it using awk ? awk has a lot of features but its a little tough to write as code the thing that you are exactly trying to do.
http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/
You can check this link for possible examples...
Use Awk.
{
printf("RAM[%d] = '%s;\n", NR - 1, $0)
}
(Put that in foo.awk, then run awk -f foo.awk on your data.)
This is very easy to do with AWK. Use substring and $NR

Resources