Build a pyramid of stars using map function in javascript
You can use map
, yes, but I think string repeat
based on the index is the bigger challenge. As you map
, keep track of the index and pad your stars on the left and right accordingly:
function pyramid(h) { return Array(h).fill('*') .map((s, i) => ' '.repeat(h - i - 1) + s.repeat(i + 1).split('').join(' ') + ' '.repeat(h - i - 1)) .join('\n');}
console.log(pyramid(3));console.log(pyramid(4));console.log(pyramid(5));
Printing Simple Diamond Pattern in Python
Since the middle and largest row of stars has 9 stars, you should make n
equal to 9. You were able to print out half of the diamond, but now you have to try to make a function that prints a specific number of spaces, then a specific number of stars. So try to develop a pattern with the number of spaces and stars in each row,
Row1: 4 spaces, 1 star, 4 spaces
Row2: 3 spaces, 3 stars, 3 spaces
Row3: 2 spaces, 5 stars, 2 spaces
Row4: 1 space, 7 stars, 1 space
Row5: 0 spaces, 9 stars, 0 spaces
Row6: 1 space, 7 stars, 1 space
Row7: 2 spaces, 5 stars, 2 spaces
Row8: 3 spaces, 3 stars, 3 spaces
Row9: 4 spaces, 1 star, 4 spaces
So what can you deduce? From row 1 to (n+1)/2, the number of spaces decreases as the number of stars increase. So from 1 to 5, the # of stars
= (row number
* 2) - 1, while # of spaces before stars
= 5 - row number
.
Now from row (n+1)/2 + 1 to row 9, the number of spaces increase while the number of stars decrease. So from 6 to n, the # of stars
= ((n+1 - row number
) * 2) - 1, while # of spaces before stars
= row number
- 5.
From this information, you should be able to make a program that looks like this,
n = 9
print("Pattern 1")
for a1 in range(1, (n+1)//2 + 1): #from row 1 to 5
for a2 in range((n+1)//2 - a1):
print(" ", end = "")
for a3 in range((a1*2)-1):
print("*", end = "")
print()
for a1 in range((n+1)//2 + 1, n + 1): #from row 6 to 9
for a2 in range(a1 - (n+1)//2):
print(" ", end = "")
for a3 in range((n+1 - a1)*2 - 1):
print("*", end = "")
print()
Note that you can replace n with any odd number to create a perfect diamond of that many lines.
How to check if a String contains another String in a case insensitive manner in Java?
Yes, contains is case sensitive. You can use java.util.regex.Pattern with the CASE_INSENSITIVE flag for case insensitive matching:
Pattern.compile(Pattern.quote(wantedStr), Pattern.CASE_INSENSITIVE).matcher(source).find();
EDIT: If s2 contains regex special characters (of which there are many) it's important to quote it first. I've corrected my answer since it is the first one people will see, but vote up Matt Quail's since he pointed this out.
Related Topics
How Get Total Sum from Input Box Values Using JavaScript
Print Function Not Working in Chrome
How to Only Trigger Parent Click Event When a Child Is Clicked
Why React Props Are Passed Undefined to the Child Component
React Wait Until Set State in the Same Function
How to Find Matching Values in Two Arrays
Angular - Displaying Object Within Object in Json
Blocking Device Rotation on Mobile Web Pages
How to Extract Data from Json Nested Objects
How to Check Whether Session Is Null or Not in JavaScript
Retrieving Keys from Json Array Key-Value Pair Dynamically - JavaScript
Pass Dynamic Object in Onclick - Javascript/Jquery
How to Open a File/Browse Dialog Using JavaScript
Angular 4: How to Read Content of Text File With Httpclient
Pass Data from HTML Table to Modal and Then to PHP Script
How to Force Clients to Refresh JavaScript Files
How Exactly Use and Update the Global Variables in React-Native