[Solved] How to con...
 
Notifications
Clear all

[Solved] How to control implicit conversion from long to int?

0 Posts
1 Users
0 Likes
5 Views
0
Topic starter
Armen Haddad Asks: How to control implicit conversion from long to int?
I am working on this LeetCode problem to take an integer and reverse it, given that the reversed in is within the signed 32-bit range, in which case we should return 0.

and this code is doing just that, even with numbers like 1534236469/-1534236469. Except when it comes to tricky numbers like -2147483648 where its not recognising it as out of range and instead returning 8 and not 0.

I know this is not the cleanest code, but can you help me recognise what I'm missing?

Code:
#include<iostream>
#include<limits>
using namespace std;

class Solution {
public:
    int reverse(int x) {
        int a, r, y;
        string num, fnum;
        a = abs(x);
        try{
            while(a != 0){
                r = a % 10;
                a = a / 10;
                num = to_string(r);
                fnum = fnum + num;
                y = stoi(fnum);
            }
        } catch(out_of_range& oor){
            return 0;
        }
        if(x==0){
            return 0;
        } else if (x<0){
            return -y;
        } else {
            return y;
        }
    }
};

int main(){
    Solution mine;
    cout << mine.reverse(-2147483648);
}

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.

 

 


 

Unreplied Posts

[Solved] How to animate a rectangle traveling between buttons headings?

TruMan1 Asks: How to animate a rectangle traveling between buttons headings?
I have a group of buttons that behave like a segmented picker. As you tap a button, it updates an enum in state. I’d like to show an indicator on top that runs between buttons instead of show/hide.

This is what I have:

Code:
struct ContentView: View {
    enum PageItem: String, CaseIterable, Identifiable {
        case page1
        case page2

        var id: String { rawValue }

        var title: LocalizedStringKey {
            switch self {
            case .page1:
                return "Page 1"
            case .page2:
                return "Page 2"
            }
        }
    }

    @Namespace private var pagePickerAnimation
    @State private var selectedPage: PageItem = .page1

    var body: some View {
        HStack(spacing: 16) {
            ForEach(PageItem.allCases) { page in
                Button {
                    selectedPage = page
                } label: {
                    VStack {
                        if page == selectedPage {
                            Rectangle()
                                .fill(Color(.label))
                                .frame(maxWidth: .infinity)
                                .frame(height: 1)
                                .matchedGeometryEffect(id: "pageIndicator", in: pagePickerAnimation)
                        }
                        Text(page.title)
                            .padding(.vertical, 8)
                            .padding(.horizontal, 12)
                    }
                    .contentShape(Rectangle())
                }
            }
        }
        .padding()
    }
}

enter image description here

I thought the matchedGeometryEffect would help do this but I might be using it wrong or a better way exists. How can I achieve this where the black line on top of the button animates over one button and moves over the other?

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.
 
 
 

[Solved] Verify Geodatabase Integrity?

hedz09 Asks: Verify Geodatabase Integrity?
I’m regularly backing up an ArcSDE Geodatabase. The backup is done by using arcpy.Copy_management to copy datasets from SDE to a local File GeoDatabase.

How can I verify the integrity of the back up and ensure that nothing is corrupt?

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.

 

 


 

[Solved] Shapely polygons crossing the antimeridian

Adam Matan Asks: Shapely polygons crossing the antimeridian

Sample data​

Consider the following WKT Polygon, crossing the international dateline (antimeridian):

POLYGON((176 49,-65 49,-65 11,176 11,176 49))


Polygon crossing the dateline

And the following points:

Code:
POINT(-140 32) # Inside the polygon
POINT(0 32)    # Outside the polygon


Points inside and outside the polygon

The problem​

Shapely considers this polygon to span on the other side of the planet – covering Asia and the Atlantic, rather than the US and the Pacific. Therefore, it fails to calculate its centroid and tell whether points are inside or outside it:

Code:
from shapely import wkt

polygon_wkt = 'POLYGON((176 49,-65 49,-65 11,176 11,176 49))'
point_in_polygon_wkt = 'POINT(-140 32)'
point_outside_polygon_wkt = 'POINT(0 32)'

polygon = wkt.loads(polygon_wkt)
point_in_polygon = wkt.loads(point_in_polygon_wkt)
point_outside_polygon = wkt.loads(point_outside_polygon_wkt)


print polygon.centroid                         # POINT (55.5 30) - Wrong!
print polygon.contains(point_in_polygon)       # False - Wrong!
print polygon.contains(point_outside_polygon)  # True - Wrong!

What have I tried​

  • Using PostGIS – I get the same erroneous results.
  • Playing with Shapely arguments – couldn’t manage to “wrap” the polygon to the other side of the planet.
  • Reading The International Date Line wrap around. To be frank, there does not seem to be an answer there (Except for splitting the polygon).

My question​

How can I calculate the centroid, bounding box, and inside/outside predicate for a WGS84 polygon optionally crossing the international dateline (longitude 180 / -180)?

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.

 

 


 

[Solved] Nftables management problem

Rimvydas Asks: Nftables management problem
I’ve migrated from iptables to nftables and have one annoying problem. In the old system I had a script which removed/added a few rules each day. I was able to easily add/delete iptables rules by their position, which was CONSTANT.

Now we have annoying handles. When I add a rule from the script, it gets assigned by dynamic handle number. I can delete rule by retrieving this handle number at first. The problem is that when the same rule gets deleted and re-added. after that, its handle number gets incremented, so is not the same. So I’m not able to use the same simple approach as I had in my old system.

Can anyone give me a solution for this problem – chain has at least 40 rules in it, and I want to delete re-add the same rule several rules each day. It was so easy with iptables, and now I can’t find easy solution

Thanks.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.

 

 


 

[Solved] Correct way to switch back to iptables on debian 10 or 11

user3450548 Asks: Correct way to switch back to iptables on debian 10 or 11
With debian 10 and 11 nftables is the new firewalling framework.

I have some machines that rely heavily on a old custom iptables config, aside from that I can easily move everything to new debian 10/11 installations or upgrading the previous ones, no problem with most of other services being updated to current versions.

Now, while I get myself a certain amount of time to learn nftables (because I want to learn it decently and tweak it finely to my needs*):

What could be a right way to disable nftables and use iptables + iptables-persistent for now ?

(I would like to take advantage of all the security updates and fixes debian 10 carries with it while getting my time to slowly move to nftables.)

ALSO BONUS

I would like to do the opposite once I finished my nftables configuration so: how to switch back to nftables and disable iptables ?

In some sorts I’m asking basically how to switch back and forth between iptables and nftables on debian 10 and 11 ?

*I know that there’s an utility to help move from iptables to nftables syntax but I prefer don’t rely on automatic conversion. So for now I prefere put iptables back in place and slowly change the rules manually.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.

 

 


 

[Solved] Linux mount point in proc virtual filesystem?

HermannSW Asks: Linux mount point in proc virtual filesystem?
I was asked to move this question here from stackoverflow.

I did fork guitmz’s memrun repo (for asm and go).
I did provide memrun and memfd_create for C in my fork:
GitHub – Hermann-SW/memrun: Small tool to run ELF binaries from memory with a given process name

memfd_create.c creates a memory file (/memfd:…) and returns process pid and memory filedescriptor:

Code:
pi@raspberrypi400:~/memrun/C $ gcc memfd_create.c -o memfd_create
pi@raspberrypi400:~/memrun/C $ ./memfd_create
1880 3
pi@raspberrypi400:~/memrun/C $ ls -l /proc/1880/fd
total 0
lrwx------ 1 pi pi 64 Oct  6 20:54 0 -> /dev/pts/0
lrwx------ 1 pi pi 64 Oct  6 20:54 1 -> /dev/pts/0
lrwx------ 1 pi pi 64 Oct  6 20:54 2 -> /dev/pts/0
lrwx------ 1 pi pi 64 Oct  6 20:54 3 -> '/memfd:rab.oof (deleted)'
pi@raspberrypi400:~/memrun/C $

Creating 10MB filesystem in memory file works:

Code:
pi@raspberrypi400:~/memrun/C $ dd if=/dev/zero of=/proc/1880/fd/3 bs=1024 count=10240 2> /dev/null
pi@raspberrypi400:~/memrun/C $ mkfs.ext2 /proc/1880/fd/3 > /dev/null
mke2fs 1.44.5 (15-Dec-2018)
pi@raspberrypi400:~/memrun/C $

And mounting that filesystem under /proc(!) works, on 32bit Raspberry Pi OS (debian) as well as on 64bit Intel Ubuntu:

Code:
pi@raspberrypi400:~/memrun/C $ ls -l /proc/1880/fd
total 12
drwx------ 2 root root 12288 Oct  6 20:56 lost+found
pi@raspberrypi400:~/memrun/C $

The same mount under /proc does not work on Red Hat Enterprise Linux:

Code:
$ ./memfd_create
26611 3
$ ls -l /proc/26611/fd
total 0
lrwx------. 1 stammw stammw 64 Oct  6 21:00 0 -> /dev/pts/0
lrwx------. 1 stammw stammw 64 Oct  6 21:00 1 -> /dev/pts/0
lrwx------. 1 stammw stammw 64 Oct  6 21:00 2 -> /dev/pts/0
lrwx------. 1 stammw stammw 64 Oct  6 21:00 3 -> '/memfd:rab.oof (deleted)'
$ 
$ dd if=/dev/zero of=/proc/26611/fd/3 bs=1024 count=10240 2> /dev/null
$ mkfs.ext2 /proc/26611/fd/3 > /dev/null
mke2fs 1.45.6 (20-Mar-2020)
$ 
$ sudo mount /proc/26611/fd/3 /proc/26611/fd
[sudo] password for stammw: 
mount: /proc/26611/fd: cannot mount /dev/loop0 read-only.
$

Should mounting under /proc not work at all?
In case it should, what is needed to mount successfully under /proc for RHEL?

Perhaps one word on why I do that: tcc “-run” option enhanced g++/gcc, with all gcc/g++ temporary files as well as executable in RAM, and executed from RAM:

Code:
pi@raspberrypi400:~/memrun/C $ fortune -s | bin/g++ -run demo.cpp foo 123
bar foo
Sorry.  I forget what I was going to say.
pi@raspberrypi400:~/memrun/C $ 
pi@raspberrypi400:~/memrun/C $ cat demo.cpp 
/**
*/
#include <iostream>

int main(int argc, char *argv[])
{
  printf("bar %sn", argc>1 ? argv[1] : "(undef)");

  for(char c; std::cin.read(&c, 1); )  { std::cout << c; }

  return 0;
}
pi@raspberrypi400:~/memrun/C $

A first answer from the other thread:

Your RHEL output shows a . after the permissions, meaning there are additional SELinux permissions at play. Can you see what they are? – that other guy

On RHEL:

Code:
$ sudo ls -Z /proc/26611/fd
[sudo] password for stammw: 
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 0
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 2
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 3
$
$ ls -Z /proc/26611 | grep fd$
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 fd
$

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.

 

 


 

[Solved] Combine .gz file with .tsv file (columns) using command line

akaDrHouse Asks: Combine .gz file with .tsv file (columns) using command line
I have two files.

  1. .gz file with with dimensions 12m x 5 columns
  2. .tsv file (no compression) with the same 12m rows and 9 more columns

I normally would do this in R where I would read in both objects, use cbind() to bind file 1 and file 2 columns -> file with 12m rows and 14 columns. Then I would write out a compressed .gz file with fwrite().

I’m thinking that this should be able to be done faster at the command line, but I’m not sure if .gz has to be uncompressed with zcat or not. I’m looking for the fastest solution to do something like this where the output file is also gzip compressed.

paste file1.gz file2.tsv > combinedfile.gz

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.

 

 


 

[Solved] Array Declaration: Double Quotes & Parentheses

ebsf Asks: Array Declaration: Double Quotes & Parentheses
Perhaps this is a stupid question but two hours on Google hasn’t turned up anything on point.

Simply, does a difference exist in Bash between:

Code:
X="
a
b
c
"

and

Code:
X=(
a
b
c
)

The former conforms with the definition of a variable, the latter, the definition of an array.

An array is a multi-element variable, so is this to say that the former also is an array for all purposes?

If the former is an array, is the only difference in operation as between (a) the double quotes and (b) the parentheses, the operation of quoting rules on the array’s elements?

Many thanks for any insights.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.

 

 


 

[Solved] Cannot install ghostscript, libgs9 and libpaper1 on Debian Bullseye

Kfcaio Asks: Cannot install ghostscript, libgs9 and libpaper1 on Debian Bullseye
My Dockerfile is:

Code:
FROM python:3.9-slim-bullseye


RUN apt-get update && apt-get install 
    --no-install-recommends -qq -y 
    apt-utils 
    poppler-utils 
    libjpeg-dev 
    libcairo2-dev 
    pdftk 
    ghostscript 
    autoconf-archive 
    pkg-config 
    libpng-dev 
    libtiff5-dev 
    zlib1g-dev 
    imagemagick 
    wget 
    procps 
    curl 
    locales 
    libicu-dev 
    libpango1.0-dev 
    g++ 
    make 
    tesseract-ocr 
    tesseract-ocr-por 
    tesseract-ocr-osd

I can build it through sudo docker build -t foo . on my machine (Ubuntu 20.04 and Docker version 19.03.10), but I can’t do the same on Travis CI:

Code:
Errors were encountered while processing:
 libpaper1:amd64
 libgs9:amd64
 ghostscript
E: Sub-process /usr/bin/dpkg returned an error code (1)
The command '/bin/sh -c apt-get update && apt-get install     --no-install-recommends -qq -y     apt-utils     poppler-utils     libjpeg-dev     libcairo2-dev     pdftk     ghostscript     autoconf-archive     pkg-config     libpng-dev     libtiff5-dev     zlib1g-dev     imagemagick     wget     procps     curl     locales     libicu-dev     libpango1.0-dev     g++     make     tesseract-ocr     tesseract-ocr-por     tesseract-ocr-osd     libc6     libgs9     ucf     libpaper1' returned a non-zero code: 100

Part of log regarding libpaper1:

Code:
Setting up libpaper1:amd64 (1.1.28+b1) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
Creating config file /etc/papersize with new version
stat: cannot statx '/etc/papersize.dpkg-inst': Operation not permitted
dpkg: error processing package libpaper1:amd64 (--configure):
 installed libpaper1:amd64 package post-installation script subprocess returned error exit status 1

Part of log regarding libgs9:

Code:
dpkg: error processing package libgs9:amd64 (--configure):
 dependency problems - leaving unconfigured

Part of log regarding ghostscript:

Code:
dpkg: dependency problems prevent configuration of ghostscript:
 ghostscript depends on libgs9 (= 9.53.3~dfsg-7+deb11u1); however:
  Package libgs9:amd64 is not configured yet.

I tried the following without success:

Code:
RUN dpkg --configure -a && apt-get -f install

and also:

Code:
RUN apt-get clean && apt-get update && apt-get install --fix-broken

RUN apt-get install --assume-yes --no-install-recommends -qq 

Part of my .travis.yaml:

Code:
jobs:
  include:
    - stage: Build temporary docker image
      services:
        - docker
      language: minimal
      if: type = pull_request AND branch = master
      script:
        - source ci_scripts/gcloud_setup.sh
        - gcloud docker -- pull gcr.io/foo/bar
        - docker build --cache-from gcr.io/foo/bar:latest -t bar_temp .
        - docker tag bar_temp gcr.io/foo/bar
        - gcloud docker -- push gcr.io/foo/bar

Am I missing something here? How can I install those packages without error?

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.

 

 


 

[Solved] Reverse the autohide dock behavior on Gnome 40

Jared Peter Asks: Reverse the autohide dock behavior on Gnome 40
I’d like to make the dock (dash to dock) stay shown even when a window is maximized, and hidden when I hover over it for more than two seconds.

I used a workaround from this question but that only forces the dock to stay fixed. I also need the dock to hide if I hover over it for more than two seconds

Is it possible to change that in their code somewhere? I don’t want to turn autohide off, all I want is to reverse its behavior.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.

 

 


 

Share: