Can you append to specific elements in an array based on if statement conditions?
def pig_latina(word)
prefix = word[0, %w(a e i o u).map{|vowel| "#{word}aeiou".index(vowel)}.min]
prefix = 'qu' if word[0, 2] == 'qu'
prefix.length == 0 ? "#{word}way" : "#{word[prefix.length..-1]}#{prefix}ay"
end
phrase = "The dog jumped over the quail"
translated = phrase.scan(/\w+/).map{|word| pig_latina(word)}.join(" ").capitalize
puts translated # => "Ethay ogday umpedjay overway ethay ailquay"
JavaScript - prevent push of certain element into an array if condition is true
Just add this to if statement
for (var i = 0; i < userProfile.length; i++) {
if (userProfile[i].userGenre == me.meGenre && userProfile[i].userid != me.meUserid) {
potentialMatches.push(userProfile[i]);
}
}
Can I add items to a slice in the creation statement depending on a condition?
No, you can't have conditional inclusion of listed elements in a composite literal.
And it may be more verbose using an additional if
and append()
, but it's much more obvious what happens (what your code does).
You could achieve something like this using a helper function passing the condition and all the elements, but that just obfuscates the code more and would have worse performance.
How to add elements to an array based on a condition?
You can reduced increment count by 1, when user inputs wrong/no number.
Also note, you are code currently reading input only for 4(not 5 as question description says.) numbers.
int[] numbers = new int[4];
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Enter a number: ");
string c = Console.ReadLine();
int value;
if (int.TryParse(c, out value))
{
numbers[i] = value;
}
else
{
i--;
Console.WriteLine("You did not enter a number\n");
}
}
for (int i = 0; i < numbers.Length; i++ )
{
Console.Write(numbers[i] + " ");
}
Python 3 using an if statement to read the values in 1 array and append another value to another array
You're comparing i, your loop counter to your grade validator.
You want the loopcounter to say which item of the array to call out.
So if i = 0 in one instance, it's looking to see if 0 is greater than 70 and less than 100.
If i = 5, and you're calling percent[i], you're getting the sixth value in the array percent, 88.
percent = [33, 44, 55, 66, 77, 88, 99]
grade = []
for i in range(0, len(percent)):
if percent[i] >= 70 and percent[i] <= 100:
grade.append("Grade A")
elif percent[i] >= 60 and percent[i] <= 69:
grade.append("Grade B")
elif percent[i] >= 50 and percent[i] <= 59:
grade.append("Grade C")
elif percent[i] >= 45 and percent[i] <= 49:
grade.append("Grade D")
else:
grade.append("No grade achieved")
print(grade)
using an array as a condition in an if loop
You can use any
builtin function:
Given:
v1 = [[-2.736, -0.466, -2.248], [-3.373, -2.93, -1.288], [-0.765, -3.666, 1.123], [-1.269, 1.882, 1.073], [3.444, 1.159, -3.183], [3.339, 2.289, 0.759], [-4.057, 2.649, -0.189], [0.317, 1.196, -3.699], [0.0, 0.0, 0.0]]
for numi,i in enumerate(v1):
if any(v in ala or v in gly for v in i):
continue
else:
for numj,j in enumerate(v1):
if any(v in ala or v in gly for v in j)::
continue
else:
angle=(np.arccos(np.dot(i,j))/(np.linalg.norm(i)*np.linalg.norm(j))))*180/(np.pi)
print(angle)
appending an array in while loop and if condition
You are reinitialising the list dgt
for every loop iteration. Move it outside the loop.
n = -12
dgt = []
while n < 15:
if n < 0:
dgt.append(n)
n = n+1
print(dgt)
Related Topics
Ruby - How to Retrieve Sum in Array Group by Multiple Keys with Condition Max
How to Verify Pacts Against an API That Requires an Auth Token
Utc Time Resets to 2000-01-01 (Ruby). How to Prevent the Time from Resetting
Don't the Ruby Methods Instance_Eval() and Send() Negate the Benefits of Private Visibility
Rails 4 Order by Virtual Attribute
Count Overlapping Regex Matches in Perl or Ruby
How to Ignore Irrelevant Methods When Profiling Ruby Applications
Instagram Ruby Gem - Unable to Reach Callback Url
Enter & Ioerror: Byte Oriented Read for Character Buffered Io
Connection Refused Using Sunspot and Solr in Rails
Rails 4 Use Application Helpers Inside Initializers
How to Capture Terminal Arrow Keys in Ruby
Rails 3 + Daemons Gem: Exception When Querying Model
Ruby Instance Method & Conditional Local Variable Assignment with Same Name
Numeric Literals Prepended with '0'