程序员问答大本营 98sky.com.


10 questions online user: 19

0
votes
answers
0 views
+10

How to find sublist which present one list not another list in python? [duplicate]

This question already has an answer here:

I need to compare two lists which are basically list-of-list find out the sublists which are present in one list but not other. Also the arrangement of the sublists does not consider i.e. ['a','b'] = ['b,'a']. The two lists are

List_1 = [['T_1','T_2'],['T_2','T_3'],['T_1','T_3']]
List_2 = [['T_1','T_2'],['T_3','T_1']]

The output list should be

out_list = [['T_2','T_3']]
0
votes
answers
0 views
+10

VBA: Change value of variables in an array

How do I permanently change the value of a variable in an array? I wrote some code that illustrates my problem:

Option Explicit
    Dim var1 As Boolean, var2 As Boolean, var3 As Boolean

Sub Test()
    Dim arrTest As Variant
    Dim i

    arrTest = Array(var1, var2, var3)

    For Each i In arrTest
        Debug.Print i
    Next

    For i = LBound(arrTest) To UBound(arrTest)
        arrTest(i) = True
    Next

    For Each i In arrTest
        Debug.Print i
    Next

    Test2
End Sub


Sub Test2()
    Debug.Print "var1 in Sub Test2 : " & var1
    If var1 Then
        Debug.Print "Hello"
    End If
End Sub

The output in the direct window is:

False
False
False
True
True
True
var1 in Sub Test2 : False

The first six lines make sense to me because I changed the value of the variables in Test. However, it obviously wasn't permanent or it was confined to that one sub and so "Hello" in Test2 didn't get printed.

How do I change this? I've read that for each loops are read-only, but why does the for-loop not work?

0
votes
answers
0 views
+10

Allocate contiguous memory for a 3D array in C++

I'm able to allocate contiguous memory to allocate a 2D array in C++. I do not know how to do for a 3D array. I have already read some posts but I haven't been able to come up with a solution.

#include <iostream>


using namespace std;

int main(int argc, char **argv){
    cout << "Ints have size " << sizeof(int) << endl;

int rows= 2;
int cols= 3;

int **i= new int*[rows];
int size= rows*cols;
i[0]= new int[size];
for(int j= 1; j < rows; j++) {
  i[j]= &i[0][j*cols];
}

for(int j= 0; j < rows; j++) {
  for(int k= 0; k < cols; k++) {
    cout << j << " " << k << " " << &i[j][k] << endl;
  }
}
delete[] i;
return 0;
}
0
votes
answers
0 views
+10

Java streams: count distinct values in array of primitives

Why does a distinct count of an int array return a different result than a count of an Integer array? I would expect a result of 3 in both cases.

int[] numbers1 = { 1, 2, 3 };
System.out.println("numbers1: " + Arrays.toString(numbers1));
System.out.println("distinct numbers1 count: " + Stream.of(numbers1).distinct().count());

Integer[] numbers2 = { 1, 2, 3 };
System.out.println("numbers2: " + Arrays.toString(numbers2));
System.out.println("distinct numbers2 count: " + Stream.of(numbers2).distinct().count());

Results

numbers1: [1, 2, 3]
distinct numbers1 count: 1

numbers2: [1, 2, 3]
distinct numbers2 count: 3
0
votes
answers
0 views
+10

Why is the size of this union larger than expected?

When I type in the following code, I get a result of 32 bytes. It supposed to be 28 bytes, though. Why do I get that result?

//size of union variable

union U {
    int x[7];
    double y[3];
}z;

int main(){
    printf("%d",sizeof(z));
    return 0;
}
0
votes
answers
0 views
+10

Why is the DOM not updating with state change in ReactJS?

I want to update the font size of an element using a slider. When I slide the slider, the value changes and I can successfully store that into my state using e.target.value.

But the problem is the font size does not update as I expect. I can't figure out why?

Here is my React component code:

import React, { useState } from 'react';

function App() {
  const [slider, setSlider] = useState(20);

  const handleChange = (e) => {
    setSlider( e.target.value );
    console.log(slider);
  }

  return (

    <div className="container">
        <label style={ { fontSize: slider } }>Example range</label>
        <input 
          type="range" 
          className="custom-range" 
          id="customRange1"
          min="10" 
          max="30"
          defaultValue={slider}
          onChange={handleChange}
        /> 
    </div>
  );
}
export default App;
0
votes
answers
0 views
+10

React - How to change code written in the old way when using a new approach?

I was looking for steps on the internet to refactor previous applications for a new approach, but I did not find a satisfactory answer...

I know the previous approach for creating applications in ReactJS which uses the Component and render() function for example, but I can see that it is currently different because:

npx create-react-app app-name

is now generating a different template.

For example, previously it was imported:

import React, {Component} from 'react';

and now only:

import React from 'react';

I am asking for tips or simple advice on what I should change so that the old code works without importing the Component.

Has the method of using functions for communication via AJAX (e.g. loadPerson) changed as well?

For example here is some not working example of ./src/PersonDetail.js:

import React from 'react';
import { DetailList } from './DetailList';
import { loadPerson } from './requests';

export class PersonDetail {
  constructor(props) {
    super(props);
    this.state = {person: null};
  }

  async componentDidMount() {
    const {personId} = this.props.match.params;
    const person = await loadPerson(personId);
    this.setState({person});
  }

  render() {
    const {person} = this.state;
    if (!person) {
      return null;
    }
    return (
      <div>
        <h1 className="title">{person.name}</h1>
        <div className="box">{person.description}</div>
        <h5 className="title is-5">Details at {person.name}</h5>
        <DetailList details={person.details} />
      </div>
    );
  }
}

Thank you in advance.

0
votes
answers
0 views
+10

Why does string addition result is so weird?

public static void Main(string[] args)
{
     int num = 1;
     string number = num.ToString();
     Console.WriteLine(number[0]);
     Console.WriteLine(number[0] + number[0]);
}

I expect the output of 1 and 11 but I'm getting 1 and 98. What am I missing?

0
votes
answers
0 views
+10

Why do strings in a std::vector end up with the same data address?

Consider the following C++ program:

#include <iostream>
#include <string>
#include <vector>


int main()
{
    std::vector<std::string> v(2, std::string(24,0));
    for (auto& s : v) {
        std::cout << "Address: " << (void*)s.data() << std::endl;
    }
}

Demo

I would expect each string in the vector to point to a different memory region, but with both gcc 6.3.0 and 8.2.1 when compiling with -D_GLIBCXX_USE_CXX11_ABI=0, they show the same address. (when compiling without the flag, they show different addresses). Why is that?

0
votes
answers
0 views
+10

std::bind to a std::variant containing multiple std::function types

I'm playing around with callback functions and wish to register multiple functions via std::bind that differ in signatures (altough they all return void). Assigning the result of the std::bind to the std::variant yields in a "conversion to non-scalar type" error. Is it an ambiguity error? Can I provide the compiler with more information?

Dropping the std::bind (which allows the assignment) is not an option as I wish to register the callbacks using some

template <typename Function, typename... Args>
void register(Function &&f, Args &&... args)
{
    variant_of_multiple_func_types = std::bind(f, args...);
}

For example:

std::variant<std::function<void()>, int> v = std::bind([]() noexcept {});

works, but

std::variant<std::function<void()>, std::function<void(int)>> v = std::bind([]() noexcept {});

does not, while I expect it to compile into a std::variant containing a std::function<void()>.

I get the following compilation error in GCC 7.4.0 with -std=c++17:

error: conversion from ‘std::_Bind_helper<false, main(int, char**)::<lambda()> >::type {aka std::_Bind<main(int, char**)::<lambda()>()>}’ to non-scalar type ‘std::variant<std::function<void()>, std::function<void(int)> >’ requested
     std::variant<std::function<void()>, std::function<void(int)>> v = std::bind([]() noexcept {});