I have a table of numbers that are in an array that have gotten mapped and now I'm trying to present them right aligned for example I have this:
[1,2,3,4,5,6]
[1,2,44,5,66,77]
But want this:
1 2 3 4 5 6
1 2 44 5 66 77
Not sure if its coming through but I don't want the brackets or quotes if the values were a string BUT I want them right aligned vs left aligned. I figured out left aligned and just trying to see if there is an easy way to do this.
var arr= [0,1,2,3]
for i in 0...3 {
let table = arr.map { $0 * i }
print (table)
}
You are simply printing the array and the description method of Array will show the list of values separated by commas with the brackets.
If you want any other output you need to generate it yourself.
Replace your current print with the following:
let line = table.map { String(format: "%4d", $0)}.joined()
print(line)
This maps the array of Int into an array of String and then joins those strings into a single string with no separator between them. Each Int is formatted into a String that will take four spaces and the number will be right-aligned within those four spaces. Adjust as needed.
Related
I want to extract a number that follows a specific string ':' and write a code that adds that number. I think.. can split it by space and extract it from it... Well, it doesn't work.
1.(12321 6,80.0:3 210.1:3!!!73 540.2:1++ 96.3:3!<<<<%% 689.4:3 24.5:4)
I want to extract the number 3 3 1 3 3 3 4 followed by ":" from this string and find out that the sum is 17.
import re
var1 = '1.(12321 6,80.0:3 210.1:3!!!73 540.2:1++ 96.3:3!<<<<%% 689.4:3 24.5:4)'
item = var1.split(" ")
sum([int(i) for i in re.findall('(?<=:)\\d+',var1)])
17
This question already has answers here:
Number of words in a Swift String for word count calculation
(7 answers)
Closed 5 years ago.
Edit: there is already a question similar to this one but it's for numbers separated by a specific character (Get no. Of words in swift for average calculator). Instead this question is about to get the number of real words in a text, separated in various ways: a line break, some line breaks, a space, more than a space etc.
I would like to get the number of words in a string with Swift 3.
I'm using this code but I get imprecise result because the number is get counting the spaces and new lines instead of the effective number of words.
let str = "Architects and city planners,are \ndesigning buildings to create a better quality of life in our urban areas."
// 18 words, 21 spaces, 2 lines
let components = str.components(separatedBy: .whitespacesAndNewlines)
let a = components.count
print(a)
// 23 instead of 18
Consecutive spaces and newlines aren't coalesced into one generic whitespace region, so you're simply getting a bunch of empty "words" between successive whitespace characters. Get rid of this by filtering out empty strings:
let components = str.components(separatedBy: .whitespacesAndNewlines)
let words = components.filter { !$0.isEmpty }
print(words.count) // 17
The above will print 17 because you haven't included , as a separation character, so the string "planners,are" is treated as one word.
You can break that string up as well by adding punctuation characters to the set of separators like so:
let chararacterSet = CharacterSet.whitespacesAndNewlines.union(.punctuationCharacters)
let components = str.components(separatedBy: chararacterSet)
let words = components.filter { !$0.isEmpty }
print(words.count) // 18
Now you'll see a count of 18 like you expect.
I currently have a 4x3500 cell array. First row is a single number, 2 row is a single string, 3rd and 4th rows are also single numbers.
Ex:
1 1 2 3 3 4 5 5 5 6
hi no ya he ........ % you get the idea
28 34 18 0 3 ......
55 2 4 42 24 .....
I would like to be able to select all columns that have a certain value in the first row. ie if I wanted '1' as the first row value, it would return
1 1
hi no
28 34
55 2
Then I would like to sort based on the 2nd row's string. ie if I wanted to have'hi', it would return:
1
hi
28
55
I have attempted to do:
variable = cellArray{:,find(cellArray{1,:} == 1)}
However I keep getting:
Error using find
Too many input arguments.
or
Error using ==
Too many input arguments.
Any help would be much appreciated! :)
{} indexing will return a comma separated list which will provide multiple outputs. When you pass this to find, it's the same as passing each element of your cell array as a separate input. This is what leads to the error about to many input arguments.
You will want to surround the comma-separated list with [] to create an array or numbers. Also, you don't need find because you can just use logical indexing to grab the columns you want. Additionally, you will want to index using () to grab the relevant rows, again to avoid the comma-separated list.
variable = cellArray(:, [cellArray{1,:}] == 1)
I have a table field where the data contains our memberID numbers followed by character or character + number strings
For example:
My Data
1234567Z1
2345T10
222222T10Z1
111
111A
Should Become
123456
12345
222222
111
111
I want to get just the member number (as shown in Should Become above). I.E. all the digits that are LEFT of the first character.
As the length of the member number can be different for each person (the first 1 to 7 digit) and the letters used can be different (a to z, 0 to 8 characters long), I don't think I can SPLIT the field.
Right now, in Power Query, I do 27 search and replace commands to clean this data (e.g. find T10 replace with nothing, find T20 replace with nothing, etc)
Can anyone suggest a better way to achieve this?
I did successfully create a formula for this in Excel...but I am now trying to do this in Power Query and I don't know how to convert the formula - nor am I sure this is the most efficient solution.
=iferror(value(left([MEMBERID],7)),
iferror(value(left([MEMBERID],6)),
iferror(value(left([MEMBERID],5)),
iferror(value(left([MEMBERID],4)),
iferror(value(left([MEMBERID],3)),0)
)
)
)
)
Thanks
There are likely several ways to do this. Here's one way:
Create a query Letters:
let
Source = { "a" .. "z" } & { "A" .. "Z" }
in
Source
Create a query GetFirstLetterIndex:
let
Source = (text) => let
// For each letter find out where it shows up in the text. If it doesn't show up, we will have a -1 in the list. Make that positive so that we return the index of the first letter which shows up.
firstLetterIndex = List.Transform(Letters, each let pos = Text.PositionOf(text, _), correctedPos = if pos < 0 then Text.Length(text) else pos in correctedPos),
minimumIndex = List.Min(firstLetterIndex)
in minimumIndex
in
Source
In the table containing your data, add a custom column with this formula:
Text.Range([ColumnWithData], 0, GetFirstLetterIndex([ColumnWithData]))
That formula will take everything from your data text until the first letter.
I'm trying to join an array of objects into a string that will ultimately be used to populate the detail label of a table cell. if the text is too long it will automatcally truncate and add an elipse at the end.
I want to avoid this by checking that the string is less than say 40 characters and adding an elipse in the middle instead.
so if my array is "one", "two", "three", "four" and "five" assuming they added up to more than 40 characters and my separator is ">", it may look like "one > two > three > four..." if it were automatically truncated.
Instead I want it to look like "one > two >... > five" so I always at least the first and last item from the array.
any ideas?
I would just use a UILabel and set its lineBreakMode to UILineBreakModeMiddleTruncation. Then just use -[NSArray arrayComponentsJoinedByString:] to join the elements together.