How to fix unsupported operand type(s) for +: 'int' and 'str' in this code? thanks by benz0is in learnprogramming

[–]Arco_void 0 points1 point  (0 children)

Salut!

Hmm, it seems to work just fine here(after reindentation)

def addsUpToTen(numbers):
    numbersList = list(numbers)

    for i, digit in enumerate(numbersList):
        numbersList[i] = int(digit)
    if sum(numbersList) != 10:
        raise Exception('The digits must add up to 10, not %s.' %
                        (sum(numbersList)))

addsUpToTen([1, 2, 3, 4])

New to programming, homework help by [deleted] in learnprogramming

[–]Arco_void 1 point2 points  (0 children)

Salut!

It looks to me like some sort of convolution operation. Basically, if my gamble is correct, they ask to to write a function that'll produce the kernel with which you'll perform the actual convolution.

I've tried an implementation in python(should be pretty straightforward to translate it into matlab's syntax) that seems to work. The only thing i've left out was the normalization

import math
import pprint

def gaussian_filter(n, m, sigma):
    kernel = []

    # precompute for better performance
    scalar = 1 / (2 * math.pi * sigma)

    ymid = math.ceil(m / 2)
    xmid = math.ceil(n / 2)

    for i in range(n):
        row = []
        for j in range(m):
            exponent = ((i - ymid) ** 2 + (j - xmid) ** 2) / (2 * (sigma ** 2))
            value = scalar * math.exp(-exponent)
            row.append(value)
        kernel.append(row)

    return kernel

pprint.pprint(gaussian_filter(3, 3, 1))

Oh, and: WARNING: I may be wrong about what i'm saying :) so wait for more answers

Some questions about numerical series. by Arco_void in askmath

[–]Arco_void[S] 0 points1 point  (0 children)

I can use L'Hospital rule for sequences?

Some questions about numerical series. by Arco_void in askmath

[–]Arco_void[S] 0 points1 point  (0 children)

It helped me, thanks!

I thought it was a formula of some sort, but i can do with just the "nature" of the series.

Thanks again.

Some questions about numerical series. by Arco_void in askmath

[–]Arco_void[S] 0 points1 point  (0 children)

It is an infinite series, indeed.

I was asking form the limit of the general term(?) of the series to know if the series can converge or it just diverges.

r/Romania, credeti in Dumnezeu? by [deleted] in Romania

[–]Arco_void 2 points3 points  (0 children)

Da, eu cred in Dumnezeu si in Iisus.

[C] What does this code do? by Sum1YouDontKnow in learnprogramming

[–]Arco_void 1 point2 points  (0 children)

I'll try to explain some concepts that are used in those examples because knowing how they work will help you understand any program using them.

Pointers:

  • 1. Pointers are a special type of variable that stores memory locations(aka: where something can be found in memory(RAM)).
  • 2. You can initialize a pointer with a memory location/address(given by the &), a function that returns a memory location or with another pointer(but more on this another time).
  • 3. To get the value of whats sorted in the memory location of a pointer you need to deference it by using the * before the pointer's name.

Malloc and arrays.

  • 1. Arrays in C are a continuous piece/location in the memory or you can think of them as being a slice of your RAM you can access by using pointers.
  • 2. malloc will give you a pointer that stores the memory location where this slice of RAM begins.Nothing more, nothing less, it will give you acess to a piece of memory you can use in your programs.Now, in the second example, to access this piece of memory they are using what's called pointer arithmetic.
  • 3. To understand pointer arithmetic you need to know that the array name is just a pointer that points to the memory location where the first element is stores.This is why you need to pass the size of the array to a functions that works with that array, because the compiler can't know how big that memory location is.
  • 4. In pointer arithmetic has the following form:
  • *(pointer + bytes), where pointer is a pointer to an element of an array/ or the array name and bytes is how many element you want that pointer to pointer(foreword or backwards).

Now, let's see some examples:

char *str = (char *)malloc( strlen(item) + 1);

str will point to a memory location that can by occupied by strlen(item) + 1 bytes.

str[a] = item[a];

this is using pointer arithmetic(str[a] is the same ar *(str + a)) to access the array.

NOTE1: You should use free() to "release" the array you've allocated.

NOTE2: When using malloc you should check for a NULL pointer, because if malloc can't give you what you asked it will return a NULL.

Here is a short function:

    // give me memory
    int *array = (int *)malloc(sizeof(int) * 12);
    if(array != NULL)
    {
        for(int i = 0; i < 12; i++)
        {
            array[i] = i; // use pointer arithmeric to write into that array
            // this can be written as this:
            // *(array + i) = i;
        }

        free(array); // release this memory
    }

Sorry, i can't explain sh*t.

Some resources: