Snippets Collections
#include <stdio.h>

enum Contains_Result
{
 ABSENT,
 PRESENT
};

enum Contains_Result contains(char* cstring, char find);

int main(void)
{
    char buffer1[] = "Hello Programming 1 Students";
    char buffer2[] = "Learn to program using arrays and pointers!";
    int found_d = contains(buffer1, 'd');
 
    if (found_d == PRESENT)
    {
        printf("buffer1 contains d\n");
    }
    else
    {
        printf("buffer1 does not contain d\n");
    }
 
    found_d = contains(buffer2, 'd');
 
    if (found_d == PRESENT)
    {
        printf("buffer2 contains d\n");
    }
    else
    {
        printf("buffer2 does not contain d\n");
    }
 
 	// The following function tests your code.
	// Do not modify the following code.
	test();
	
    return 0;
}

enum Contains_Result contains(char* cstring, char find)
{
 // TODO: Insert your code here...
 while (*cstring != '\0')
    {
        if (*cstring == find)
        {
            return PRESENT;
        }
        cstring++;
    }
    return ABSENT;
}
#include <stdio.h>

void print_array(int *p_array, int num_elements);
void zero_out_array(int *p_array, int num_elements);
int main(void)
{
	int main_array[] = { 15, 24, 33, 42, 51 };

	// TODO: Insert code here...
	int num_elements = sizeof(main_array)/sizeof(main_array[0]);
	
	print_array(main_array,num_elements);
    zero_out_array(main_array,num_elements);
    print_array(main_array,num_elements);
	return 0;
}

void print_array(int *p_array, int num_elements)
{
	printf("print_array called:\n");
	// TODO: Insert code here...
	for(int i = 0; i < num_elements; i++)
	{
	    printf("%d ",p_array[i]);
	}
	printf("\n\n");
}

void zero_out_array(int *p_array, int num_elements)
{
	printf("zero_out_array called:\n\n");
	// TODO: Insert code here...
	for(int i = 0; i < num_elements; i++)
	{
	    *(p_array+i) = 0;
	}
}
#include <stdio.h>

int find_min_index(int* numbers, int length);

int main(void)
{
    int array1[] = { 1, 3, 5, 7, 9, 11 };
    int array2[] = { 2, -4, 6, -8, 10, -12, 14, -16, 4 };
    int array3[] = { 6, 4, 1, 4, 5, 3, 2 };
 
    printf("Min's index in array1 is: %d\n", find_min_index(array1, 6));
    printf("Min's index in array2 is: %d\n", find_min_index(array2, 9));
    printf("Min's index in array3 is: %d\n", find_min_index(array3, 7));
 
    return 0;
}

int find_min_index(int* numbers, int length)
{
    // TODO: Insert your code here!
    
    int min_index = 0;
    
    for(int i = 0; i < length; i++)
    {
        if( numbers[i] < numbers[min_index] )
        {
            min_index = i;
        }
    }
    return min_index;
}
#include <stdio.h>

int count_odds(int* data_array, int size);

int main(void)
{
	int data_array_1[] = { 1, 3, 5, 7, 9, 11 };
	int data_array_2[] = { 2, -4, 6, -8, 10, -12, 14, -16 };
	int data_array_3[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };

	int result_1 = count_odds(data_array_1, 6);
	printf("data_array_1 has %d odd numbers.\n", result_1);
	
	int result_2 = count_odds(data_array_2, 8);
	printf("data_array_2 has %d odd numbers.\n", result_2);
	
	int result_3 = count_odds(data_array_3, 11);
	printf("data_array_3 has %d odd numbers.\n", result_3);
	
	return 0;
}

// TODO: Insert your function definition here!
int count_odds(int* data_array, int size)
{
    int count = 0;
    
    
    for(int i =0; i < size ;i++)
    {
        if(data_array[i] > 0 && data_array[i] % 2 != 0)
        {
            count++;
        }
    }
    return count;
}
#include <stdio.h>

void cube(float* number);

int main(void)
{
	float x = 0.0f;

	printf("> ");
	scanf("%f", &x);

	// TODO: Call cube with x by reference...
	cube(&x);
	
	printf("x holds %f\n\n", x);

	return 0;
}

// TODO: Define cube function:
void cube(float* number)
{
    *number = (*number)*(*number)*(*number);
    
}
#include <stdio.h>
#include <math.h>

void compute_trig(float deg, float* sin_angle, float* cos_angle);

int main(void)
{
    float sin_result = 0.0f;
    float cos_result = 0.0f;
    float angle = 0.0f;
 
    printf("Angle? \n");
    scanf("%f", &angle);
    // TODO: Call compute_trig
    compute_trig(angle,&sin_result,&cos_result);
    
    printf("sin(%f) is %f\n", angle, sin_result);
    printf("cos(%f) is %f\n", angle, cos_result);
 
    return 0;
}

void compute_trig(float deg, float* sin_angle, float* cos_angle)
{
    float pi = 3.14159f;
    float radians = deg * pi / 180.0f;
    // TODO: Compute sine angle...
    *sin_angle = sinf(radians);
    
    // TODO: Compute cosine angle...
    *cos_angle = cosf(radians);
}
class Solution:
    def maxArea(self, height: List[int]) -> int:
        left, right = 0, len(height) - 1 
        
        maxArea = 0 
        
        while left < right: 
            heightOne = height[left] 
            heightTwo = height[right] 
            
            area = min(heightOne, heightTwo) * (right - left) 
            maxArea = max(area, maxArea) 
            
            if heightTwo >= heightOne: #if heights are equal, it doesn't matter if you move left or right pointer 
                left += 1 
            elif heightTwo < heightOne: 
                right -= 1 
            
        return maxArea 
        
        
# BRUTE FORCE 
#         maxArea = 0 

#         for i in range(len(height)): 
#             for secondI in range(i + 1, len(height)): 
#                 heightOne = height[i]
#                 heightTwo = height[secondI]
#                 area = min(heightOne, heightTwo) * (secondI - i) 
#                 maxArea = max(area, maxArea) 

#         return maxArea 
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        dummy = ListNode(0, head)
        left = dummy 
        right = head #we actually want right to be head + n 
        
        #setting right to be head + n 
        while n > 0 and right: 
            right = right.next 
            n -= 1 
        
        #this should get left at previous and right at after thanks to dummy node we inserted 
        while right: 
            left = left.next
            right = right.next 
        
        #delete node 
        left.next = left.next.next 
        
        return dummy.next 



#bottom solution is what I first came up with and works in leetcode except for if head = [1] and n = 1 which is the case I tried to account for in the second elif statement but it gave me an error saying cannot return [] even though it was fine for the first if statement 

#big O: is still O(n) just like 1st scenario 

class Solution: 
	def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
    	if head is None: 
            return []
        elif head.next is None and n == 1: 
            return []
        else: 
            current = head
            counter = 0 #will become length of LL 
            
            #to determine length of LL 
            while current is not None: 
                counter += 1 
                current = current.next 
                
            #consider adding an edge case here if n > counter 
            
            #second pass now knowing length of LL 
            previous, current = head, head 
            new_counter = 0 
            target = counter - n 
            while new_counter is not target + 1: 
                if new_counter == (target - 1): 
                    previous = current 
                if new_counter == target: 
                    after = current.next 
                    previous.next = after 
                    current.next = None 
                    break 


                current = current.next 


                new_counter += 1

            return head 











star

Sun Jun 09 2024 11:45:30 GMT+0000 (Coordinated Universal Time)

#c #pointer
star

Sun Jun 09 2024 11:33:11 GMT+0000 (Coordinated Universal Time)

#c #pointer
star

Sun Jun 09 2024 11:32:00 GMT+0000 (Coordinated Universal Time)

#c #pointer
star

Sun Jun 09 2024 11:30:52 GMT+0000 (Coordinated Universal Time)

#c #pointer
star

Sun Jun 09 2024 11:29:47 GMT+0000 (Coordinated Universal Time)

#c #pointer
star

Sun Jun 09 2024 11:28:59 GMT+0000 (Coordinated Universal Time)

#c #pointer
star

Thu Sep 22 2022 18:45:49 GMT+0000 (Coordinated Universal Time) https://leetcode.com/problems/container-with-most-water/submissions/

#python #neetcode #pointer #two
star

Fri Aug 12 2022 20:01:25 GMT+0000 (Coordinated Universal Time) https://leetcode.com/problems/remove-nth-node-from-end-of-list/submissions/

#python #linked #list #two #pointer #dummy #node

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension