How can I split an array into specified lines when printing? - swift

New to software development, I'm working through Big Nerd Ranch's book 'Swift Programming'. One of the challenges set by the book is to print an array within a dictionary that contains 15 zip codes and to have them split across three lines in groups of five. The formatting must exactly match how it appears in the book.
I have tried to solve the issue using the information taught in the book with assistance from the developer documentation.
No dice.
This is what I need to print:
Caterham has the following zip codes: [111, 111, 111, 111, 111,
222, 222, 222, 222, 222,
333, 333, 333, 333, 333]
However this is the closest I can get...
var areaCodes = ["Caterham": [111, 111, 111, 111, 111], "Coulsdon": [222, 222, 222, 222, 222], "Purley": [333, 333, 333, 333, 333]]
var caterhamCodes : [Int] = []
for (_, values) in areaCodes {
caterhamCodes += values
}
print("Caterham has the following codes: \(caterhamCodes[0...4]),")
print(" \(caterhamCodes[5...9]),")
print(" \(caterhamCodes[10...14]).")
which prints:
Caterham has the following postcodes: [111, 111, 111, 111, 111],
[222, 222, 222, 222, 222],
[333, 333, 333, 333, 333].
Essentially, I need what I have above minus some square brackets and without the punctuation after each time a block of zip codes is printed. The book specifies that I might need to use a parameter called terminator. It also mentions I might need to learn how to represent special characters in string literals.
I appreciate that this is probably a high level question with a simple answer, but I'm only a few weeks into learning, so I hope I can be forgiven for asking at least a few silly questions!

Welcome to the developer community and have a nice stay!
I am trying to put answer as simple as possible, so here is my attempt:
var areaCodes = ["Caterham": [111, 111, 111, 111, 111],
"Coulsdon": [222, 222, 222, 222, 222],
"Purley": [333, 333, 333, 333, 333]]
let codesArray = areaCodes.values.sorted(by: { $0[0] < $1[0] })
let caterhamString = "Caterham has the following zip codes: ["
var printString = caterhamString
for (index, area) in codesArray.enumerated() {
for code in area {
printString += "\(code), "
}
if index != codesArray.count - 1 {
printString += "\n" + String(repeating: " ", count: caterhamString.count)
}
}
print(printString.dropLast(2).appending("]"))
Hope this helps! Good luck!

Here is a way of getting the desired output :
let intro = "Caterham has the following zip codes: ["
let spaces = ",\n" + String(repeating: " ", count: intro.count)
let areaCodes = ["Caterham": [111, 111, 111, 111, 111], "Coulsdon": [222, 222, 222, 222, 222], "Purley": [333, 333, 333, 333, 333]]
let codes = areaCodes.values.sorted { $0[0] < $1[0] }
var output = intro +
codes.map { $0.map{ String($0) }.joined(separator: ", ") }
.joined(separator: spaces)
+ "]"
print(output)
Which prints
Caterham has the following zip codes: [111, 111, 111, 111, 111,
222, 222, 222, 222, 222,
333, 333, 333, 333, 333]

Related

How to get beacons UUID, when scanning for BLE

I am using this library https://pub.dev/packages/flutter_reactive_ble for scanning the beacon. and for broadcasting a beacon I am using an android app called beacon simulator and it has got beacon UUID, majorID and minorID for each hosted beacon.
here the UUID is '0546b93b-f113-438d-8bab-b9e766dd'
but while scanning I am unable to look out for UUID instead I am getting this response in this structure.
DiscoveredDevice(id: 14:49:2F:BB:D5:97, name: , serviceData: {}, serviceUuids: [], manufacturerData: [76, 0, 2, 21, 5, 70, 185, 59, 241, 19, 67, 141, 139, 171, 185, 231, 102, 208, 17, 119, 0, 0, 0, 0, 191], rssi: -39)
Is there any way I can get the beacon's UUID in the format '0546b93b-f113-438d-8ba...'
Or any better way to uniquely identify a device? I know we can search for the device's Bluetooth address, but that just keeps on changing.
Thanks in advance.

Gatling scala: Check that the status code belongs to a list

For a scenario I want to check that status code of the response belongs to 200-209 or 304 or 404.
I tried the following but apparently it's not supported. And I can't find my use case in the docs.
scenario("Scenario example").exec(httpRequest
.check(status.in(200 to 209, 304, 404)))
Is there a better solution other than listing the codes manually?
.check(status.in(200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 304, 404))
.check(
status.in((200 to 209) ++ List(304, 404))
)
You passed something mixed with Seq and Int.
The method in takes Seq[Int]:
.check(
status.in((201 to 209) :+ 303 :+ 304)
)

Flutter Eddystone beacon detection

Is there any Flutter package available that can listen for Eddystone UID beacons and Eddystone-URLs?
I have been looking at various packages such as beacons_plugin and flutter_beacon but I cannot seem to find any package that can receive these beacon broadcasts.
Has anyone been able to achieve this? If so, how?
Thank's #ukBaz - I am now seeing the Eddystone data. However i am trying to get the namespace id and instance data from the device.
I am using a Minew D15N BLE beacon and printing out the data..
I/flutter (27645): device id: AC:23:3F:6A:FE:85, name: , type: BluetoothDeviceType.le found! rssi: -68
I/flutter (27645): advertisementData => localName: , txPowerLevel: null, connectable: false
I/flutter (27645): advertisementData -> serviceUuids length: 1
I/flutter (27645): 0000feaa-0000-1000-8000-00805f9b34fb
I/flutter (27645): advertisementData -> manufacturerData length: 0
I/flutter (27645): advertisementData -> serviceData length: 1
I/flutter (27645): 0000feaa-0000-1000-8000-00805f9b34fb: [0, 232, 113, 28, 115, 246, 74, 253, 206, 7, 183, 227, 153, 153, 153, 153, 153, 153]
I sometimes also receive this..
advertisementData -> manufacturerData length: 1
I/flutter (27645): 76: [2, 21, 144, 156, 60, 249, 252, 92, 72, 65, 182, 149, 56, 9, 88, 165, 26, 90, 234, 95, 39, 15, 204]
Based on this documentation (not sure if this is right) I was trying this..
var naaa = ByteData.sublistView(manufacturerData, 2, 11);
But get this following error..
Uncaught Error: TypeError: null: type 'JSNull' is not a subtype of type 'List<int>'
Here is a screen shot of the UID slot that i have setup to test..
Those packages look like they are just for iBeacon which uses manufacturer data in the advertising packet. For Eddystone it uses service data. The flutter_blue library give you access to the service data. https://pub.dev/documentation/flutter_blue/latest/flutter_blue/AdvertisementData-class.html
The following answer shows how to scan and access the advertising data with flutter_blue: https://stackoverflow.com/a/63591662/7721752

Rust OSError 22, Invalid argument when writing valid data to socket

I'm struggling to understand why I'm getting this error from a part of my program which sends ICMP echo requests on the network. The starnge thing about this is that I can get it to work by letting the socket handle the IP header, but when I set the IP_HDRINCL option and give it a valid IP header, it returns EINVAL error:
initialize
using interface en0 with ip 192.168.1.126 and mac a4:83:e7:43:40:81.
Input start ip/scan range: 192.168.1.1
[45, 0, 0, 1c, 20, 1, 40, 0, 40, 1, 97, 10, c0, a8, 1, 7e, c0, a8, 1, 1, 8, 0, 22, 2a, 97, 3e, 3e, 97]
[69, 0, 0, 28, 32, 1, 64, 0, 64, 1, 151, 16, 192, 168, 1, 126, 192, 168, 1, 1, 8, 0, 34, 42, 151, 62, 62, 151]
Err(Os { code: 22, kind: InvalidInput, message: "Invalid argument" })
To help visualise what's going on, I've put dumps of the data being sent in there when it sends it. the first dump is the data in hex format and the second is just the same but in decimal format.
Here is the code which opens the socket and creates/sends/receives packets:
( Apologies for the probably overwhelming amount of code, just though I should make sure I provide enough info:) )
let response = net_tools::interface::Interface::detect();
if response.is_err() {
panic! {response};
}
let active_interface = response.unwrap();
println!(
"using interface {} with ip {} and mac {}.",
active_interface.get_name(),
active_interface.get_ip_as_struct(),
active_interface.get_mac()
);
let scan_targets = net_tools::TaregtAddresses::retrieve_from_user();
let sock = net_tools::socket_tools::Socket::new_v4().expect("Failed to open socket");
let mut i = 0;
for ipv4addr in scan_targets {
if i == 1 {
break;
}
let mut d = packet_crafter::Packet::new(
active_interface.clone(),
vec![Protocols::IP, Protocols::ICMP],
// vec![Protocols::ICMP],
);
d.ip_header.set_next_protocol(Protocols::ICMP);
d.ip_header.set_dst_ip(Some(ipv4addr.octets()));
// d.ip_header.set_tos(0b11000100);
d.finalize_headers(); // calculates checksums etc
let data = d.build();
println!("{:x?}", data);
println!("{:?}", data);
let response = sock.sendto(data.as_slice(), SocketAddrV4::new(ipv4addr, 21));
println!("{:?}", response);
i += 1;
}
println!("All data sent, receiving response:");
let mut a = [0u8; 56];
let received_data = sock.recv(&mut a);
println!("{:?}", received_data);
let v = a.to_vec();
println!("{:?}", v);
This is how I'm opening a socket, and sending data on it, notice that I call setsockopt and set IP_HDRINCL to 1 when opening it:
pub fn new_v4() -> crate::io::Result<Self> {
unsafe {
let fam = libc::AF_INET; // Set domain family to ipv4
if cfg!(target_os = "linux") {
match cvt(libc::socket(fam, libc::SOCK_RAW | SOCK_CLOEXEC, 1)) {
Ok(fd) => return Ok(Self(Fd::new(fd))),
Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {}
Err(e) => return Err(e),
}
}
let _fd = cvt(libc::socket(fam, libc::SOCK_RAW, 1))?; // 1 = setting next protocol as ICMP
let _fd = Fd::new(_fd);
_fd.set_cloexec()?;
let socket = Self(_fd);
if cfg!(target_vendor = "apple") {
let payload = &1u32 as *const u32 as *const libc::c_void;
cvt(libc::setsockopt(
*socket.as_inner(),
libc::SOL_SOCKET,
SO_NOSIGPIPE,
payload,
size_of::<libc::c_int>() as libc::socklen_t,
))?;
}
let payload = &1u32 as *const u32 as *const libc::c_void;
cvt(libc::setsockopt(
*socket.as_inner(),
libc::IPPROTO_IP,
libc::IP_HDRINCL,
payload,
size_of::<libc::c_int>() as libc::socklen_t,
))?;
Ok(socket)
}
}
pub fn sendto(&self, buf: &[u8], addr: SocketAddrV4) -> crate::io::Result<usize> {
unsafe {
let (sa, l) = sock_addr_into_raw(addr);
let n = cvt({
libc::sendto(
*self.as_inner(),
buf.as_ptr() as *const libc::c_void,
cmp::min(buf.len(), super::max_len()),
MSG_NOSIGNAL,
&sa as *const _,
l,
)
})?;
Ok(n as usize)
}
}
This is the function to convert the type of the socketAddr:
pub fn sock_addr_into_raw(s: SocketAddrV4) -> (libc::sockaddr, libc::socklen_t) {
unsafe {
let mut storage = mem::MaybeUninit::<libc::sockaddr>::uninit();
let len = mem::size_of::<SocketAddrV4>();
copy_nonoverlapping(
&s as *const _ as *const libc::sockaddr as *const _ as *const u8,
&mut storage as *mut _ as *mut u8,
len,
);
(storage.assume_init(), len as _)
}
}
So back to my point, as you can see from the first segment of code, the IP header in the packet data that I dump is a valid IP header, followed by a valid ICMP header, as far as I'm aware, I have been changing things around to ensure it's valid so correct me if its not. But anyway, I know that all the other parameters being passed into sendto are valid because when I set the IP_HDRINCL socket option to 0 and only send ICMP data then it works...
initialize
using interface en0 with ip 192.168.1.126 and mac a4:83:e7:43:40:81.
Input start ip/scan range: 192.168.1.1
[8, 0, 8a, 92, 3f, 2e, 2e, 3f]
[8, 0, 138, 146, 63, 46, 46, 63]
Ok(8)
All data sent, receiving response:
Ok(28)
[69, 0, 8, 0, 224, 108, 0, 0, 254, 1, 88, 164, 192, 168, 1, 1, 192, 168, 1, 126, 0, 0, 146, 146, 63, 46, 46, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
I've even tried things such as sending an ethernet II header in the packet when IP_HDRINCL is 1, but that still just gives the same OSError. Does anyone know why sending the IP header myself breaks it??
(btw I'm running this on Mac OS Mojave 10.14.6)

ScalaTest does not pass even if conditions are verified

Can anybody explain how sentences like this can appear in my test report?
"List(Array(109, 121, 75, 101, 121)) was not equal to List(Array(109, 121, 75, 101, 121))"
Here is the full output
Run starting. Expected test count is: 4
Ws2redisAdapterTest:
Ws2redisAdapter
- should recognize 1-arity commands
- should recognize 2-arity commands *** FAILED ***
List(Array(109, 121, 75, 101, 121)) was not equal to List(Array(109, 121, 75, 101, 121)) (Ws2redisAdapterTest.scala:15)
- should recognize N-arity commands *** FAILED ***
List(Array(109, 121, 90, 115, 101, 116), Array(48), Array(45, 49)) was not equal to List(Array(109, 121, 90, 115, 101, 116), Array(48), Array(45, 49)) (Ws2redisAdapterTest.scala:
21)
- should throw Exception if an empty string is popped
Run completed in 298 milliseconds.
Total number of tests run: 4
Suites: completed 1, aborted 0
Tests: succeeded 2, failed 2, canceled 0, ignored 0, pending 0
*** 2 TESTS FAILED ***
These are my tests in code:
package eu.codesigner.finagle.ws2redis
import org.scalatest._
class Ws2redisAdapterTest extends FlatSpec with Matchers {
"Ws2redisAdapter" should "recognize 1-arity commands " in {
val (cmd, args) = Ws2redisAdapter.adaptRedisCommand("INFO")
cmd should be("INFO")
args should be(null)
}
it should "recognize 2-arity commands " in {
val (cmd, args) = Ws2redisAdapter.adaptRedisCommand("GET myKey")
cmd should be("GET")
args should be(List("myKey".getBytes()))
}
it should "recognize N-arity commands " in {
val (cmd, args) = Ws2redisAdapter.adaptRedisCommand("ZRANGE myZset 0 -1")
cmd should be("ZRANGE")
args should be(List("myZset".getBytes(),"0".getBytes(), "-1".getBytes() ))
}
it should "throw Exception if an empty string is given" in {
a[Exception] should be thrownBy {
Ws2redisAdapter.adaptRedisCommand("")
}
}
}
Equality on arrays is only defined as reference equality (like in Java). Therefore your test fails. You can check the equality of two arrays with java.util.Arrays.equals.